Laravel 11 Update Query With Example

In Laravel 11, you can update records in the database using the Eloquent ORM or the Query Builder. Below are examples of how to perform an update query using both methods.

1. Using Eloquent ORM

Eloquent ORM provides a simple and expressive way to interact with your database. To update a record using Eloquent, you first retrieve the model instance, modify its attributes, and then call the `save()` method.

<?php
use App\Models\User;
// Retrieve the user you want to update
$user = User::find(1);
// Update the user's attributes
$user->name = 'John Doe';
$user->email = 'john.doe@example.com';
// Save the changes to the database
$user->save();
?>

Alternatively, you can use the `update()` method directly on the model:

<?php
use App\Models\User;
// Update the user with ID 1
User::where('id', 1)->update([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
?>

2. Using Query Builder

The Query Builder provides a more direct way to interact with the database. You can use the `update()` method to update records that match certain conditions.

<?php
use Illuminate\Support\Facades\DB;

// Update users where the id is 1
DB::table('users')
->where('id', 1)
->update([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
?>

3. Updating Multiple Records

You can also update multiple records at once by specifying a condition that matches multiple rows.

<?php
use App\Models\User;

// Update all users with the status 'inactive'
User::where('status', 'inactive')->update([
'status' => 'active',
]);
?>

Or using the Query Builder:

<?php
use Illuminate\Support\Facades\DB;

// Update all users with the status 'inactive'
DB::table('users')
->where('status', 'inactive')
->update([
'status' => 'active',
]);
?>

4. Incrementing/Decrementing Values

Laravel also provides convenient methods for incrementing or decrementing column values.

<?php
use App\Models\User;

// Increment the 'login_count' column by 1
User::where('id', 1)->increment('login_count');

// Decrement the 'login_count' column by 1
User::where('id', 1)->decrement('login_count');

// Increment the 'login_count' column by 5
User::where('id', 1)->increment('login_count', 5);
?>

5. Updating Timestamps

If you want to update the `updated_at` timestamp manually, you can use the `touch()` method.

<?php
use App\Models\User;

$user = User::find(1);
$user->touch(); // Updates the 'updated_at' timestamp
?>

6. Mass Assignment

If you’re using mass assignment, make sure to define the `$fillable` or `$guarded` properties in your model to protect against mass assignment vulnerabilities.

<?php
class User extends Model
{
protected $fillable = [
'name', 'email', 'status',
];
}
?>

Then you can update the model like this:

<?php
$user = User::find(1);

$user->update([
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
?>

Conclusion

These are the common ways to perform update queries in Laravel 11. Whether you prefer using Eloquent ORM or the Query Builder, Laravel provides flexible and powerful tools to interact with your database.