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
- Convert multidimensional array to single array in Laravel
- Laravel 11 project setup on localhost using breeze with blade step by step
- Get comma separated email from input array
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Get laravel version
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Create model with migration and seeder
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- How to get last year records count with month wise in Laravel
- Route group with URI prefix using middleware and route name prefixes
- Laravel file size validation not working
- How to create laravel project using composer
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to Access Array in blade laravel
- Setup laravel project with docker
- Extra Filter Query on Relationships in Laravel
- How to run a specific seeder class in laravel
- Retain selected value of select box in Laravel
- Insert dummy data in users table Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- How to make Copy or Duplicate table row in laravel
- How to check data inserted or deleted in pivot after toggle method
- Get all users except the followings users in overtrue laravel-follow
- Permission denied error while creating storage link in Laravel
- How to pass link from controller to view in laravel on ajax call