Update query in Laravel
Today, we will learn the Update query in Laravel. In this tutorial, I will show you an example of how to implement an Update query in an Existing Model. Laravel model update is one of the most functionality and we will learn when creating an application using Laravel. In this post, I will give you different methods and examples of how to use update queries in Laravel.
Update query in Laravel
Example 1
$user = User::find(1);
$user->name = "Jordan mark";
$user->save();
Example 2
In this example, We can update the model using an array with multiple values which doesn’t need to use the save() method.
$user = User::find(1);
$user->update(['name' => 'Jordan Mark', 'age' => '25']);
Example 3
In this example, We can user-update records with a conditional method.
User::where('id', '1')->update([
'name' => 'Jordan Mark',
'age' => '25'
]);
Example 4
Sometimes we don’t need to change the updated_at
column when updating the record. In this case, we can use the touch option as false so that the model will exclude it.
$user = User::find(1);
$user->update([
'name' => 'Jordan Mark',
'age' => '25'
], ['touch' => false]);