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
- Redirect from www to non www in laravel using htaccess
- Property [user] does not exist on this collection instance
- How to remove package from laravel
- 419 page expired error in Laravel
- Laravel 11 sanctum api authentication example code
- Create user in Laravel using tinker
- How to add is_activated column in database using laravel migration
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Get domain name in laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Best code example for create order in Laravel for ecommerce
- Sample .htaccess file and index.php file under public directory in laravel
- How to add dynamic page title in Laravel view
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Always load the relationship data with eager loading in Laravel
- Extract only time from datetime in laravel
- Laravel insert query not working
- How to start websocket server in laravel
- How to check column value of a record is null or not in laravel
- Method Illuminate\Database\Eloquent\Collection::appends does not exist
- Permanently delete a record in laravel
- Ajax POST request in laravel
- How to get IP address in laravel
- Laravel 9 pagination with search filter
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint