Laravel update column value by appending text
You can update column value by appending text to the existing value in laravel using concat() method. You have to pass the text with column name to the concat method in the update query builder to update the data for a specific column in the table.
Append text at the start of the column value
To update the records by appending text to the start of a column in Laravel, you have to pass the string value as a first argument and column name as a second argument to the concat method DB::raw("CONCAT('Some random text ',
body)") in update() method of the Eloquent ORM.
$update = DB::table('posts') ->where("id", $id) ->update([ 'body' => DB::raw("CONCAT('Some random text ', `body`)") ]);
In the above code example, we have used the DB::raw() method with concat() to append the text in Laravel. Here we use the where condition to get the record which we want to update and execute an update query on it by appending some random text to it.
Append text at the end of the column value
You can update the column value by appending text at the end of the column value by passing the column name as first argument and text as second argument to the concat method.
$post = Post::find($id); $post->body = DB::raw('concat(body, ' . $value . ')'); $post->save();
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- SQLSTATE: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Method Illuminate\Events\Dispatcher::fire does not exist
- How to get records in random order in laravel
- 419 page expired error in Laravel
- Ajax GET request in laravel
- How to get only time from created_at in laravel
- Remove public from url in laravel project
- InRandomOrder() method with example in laravel
- Get count of filter data, while return a small set of records
- Get Array of IDs from Eloquent Collection
- Route prefix with auth middleware in laravel
- Global scope in Laravel with example
- Laravel get single row by id
- First and last item of the array using foreach iteration in laravel blade
- How to print form data in laravel
- How to get images from AWS s3 and display in Laravel blade
- How to remove package from laravel
- Add a subselect based on relationship using withAggregate method
- How to change default timestamp fields name in Laravel
- Display data in table using foreach in Laravel
- Add class to body in laravel view
- Validation errors for multiple forms on same page Laravel
- SQLSTATE: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists