Eloquent: Getting Started
- Introduction
- Generating Model Classes
- Eloquent Model Conventions
- Retrieving Models
- Retrieving Single Models / Aggregates
- Inserting and Updating Models
- Deleting Models
- Pruning Models
- Replicating Models
- Query Scopes
- Comparing Models
- Events
Introduction
Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.
Before getting started, be sure to configure a database connection in your application's config/database.php
configuration file. For more information on configuring your database, check out the database configuration documentation.
Laravel Bootcamp
If you're new to Laravel, feel free to jump into the Laravel Bootcamp. The Laravel Bootcamp will walk you through building your first Laravel application using Eloquent. It's a great way to get a tour of everything that Laravel and Eloquent have to offer.
Generating Model Classes
To get started, let's create an Eloquent model. Models typically live in the app\Models
directory and extend the Illuminate\Database\Eloquent\Model
class. You may use the make:model
Artisan command to generate a new model:
1php artisan make:model Flight
1php artisan make:model Flight
If you would like to generate a database migration when you generate the model, you may use the --migration
or -m
option:
1php artisan make:model Flight --migration
1php artisan make:model Flight --migration
You may generate various other types of classes when generating a model, such as factories, seeders, policies, controllers, and form requests. In addition, these options may be combined to create multiple classes at once:
1# Generate a model and a FlightFactory class...2php artisan make:model Flight --factory3php artisan make:model Flight -f45# Generate a model and a FlightSeeder class...6php artisan make:model Flight --seed7php artisan make:model Flight -s89# Generate a model and a FlightController class...10php artisan make:model Flight --controller11php artisan make:model Flight -c1213# Generate a model, FlightController resource class, and form request classes...14php artisan make:model Flight --controller --resource --requests15php artisan make:model Flight -crR1617# Generate a model and a FlightPolicy class...18php artisan make:model Flight --policy1920# Generate a model and a migration, factory, seeder, and controller...21php artisan make:model Flight -mfsc2223# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...24php artisan make:model Flight --all2526# Generate a pivot model...27php artisan make:model Member --pivot28php artisan make:model Member -p
1# Generate a model and a FlightFactory class...2php artisan make:model Flight --factory3php artisan make:model Flight -f45# Generate a model and a FlightSeeder class...6php artisan make:model Flight --seed7php artisan make:model Flight -s89# Generate a model and a FlightController class...10php artisan make:model Flight --controller11php artisan make:model Flight -c1213# Generate a model, FlightController resource class, and form request classes...14php artisan make:model Flight --controller --resource --requests15php artisan make:model Flight -crR1617# Generate a model and a FlightPolicy class...18php artisan make:model Flight --policy1920# Generate a model and a migration, factory, seeder, and controller...21php artisan make:model Flight -mfsc2223# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...24php artisan make:model Flight --all2526# Generate a pivot model...27php artisan make:model Member --pivot28php artisan make:model Member -p
Inspecting Models
Sometimes it can be difficult to determine all of a model's available attributes and relationships just by skimming its code. Instead, try the model:show
Artisan command, which provides a convenient overview of all the model's attributes and relations:
1php artisan model:show Flight
1php artisan model:show Flight
Eloquent Model Conventions
Models generated by the make:model
command will be placed in the app/Models
directory. Let's examine a basic model class and discuss some of Eloquent's key conventions:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 // ...10}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 // ...10}
Table Names
After glancing at the example above, you may have noticed that we did not tell Eloquent which database table corresponds to our Flight
model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight
model stores records in the flights
table, while an AirTrafficController
model would store records in an air_traffic_controllers
table.
If your model's corresponding database table does not fit this convention, you may manually specify the model's table name by defining a table
property on the model:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The table associated with the model.11 *12 * @var string13 */14 protected $table = 'my_flights';15}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The table associated with the model.11 *12 * @var string13 */14 protected $table = 'my_flights';15}
Primary Keys
Eloquent will also assume that each model's corresponding database table has a primary key column named id
. If necessary, you may define a protected $primaryKey
property on your model to specify a different column that serves as your model's primary key:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The primary key associated with the table.11 *12 * @var string13 */14 protected $primaryKey = 'flight_id';15}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The primary key associated with the table.11 *12 * @var string13 */14 protected $primaryKey = 'flight_id';15}
In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that Eloquent will automatically cast the primary key to an integer. If you wish to use a non-incrementing or a non-numeric primary key you must define a public $incrementing
property on your model that is set to false
:
1<?php23class Flight extends Model4{5 /**6 * Indicates if the model's ID is auto-incrementing.7 *8 * @var bool9 */10 public $incrementing = false;11}
1<?php23class Flight extends Model4{5 /**6 * Indicates if the model's ID is auto-incrementing.7 *8 * @var bool9 */10 public $incrementing = false;11}
If your model's primary key is not an integer, you should define a protected $keyType
property on your model. This property should have a value of string
:
1<?php23class Flight extends Model4{5 /**6 * The data type of the primary key ID.7 *8 * @var string9 */10 protected $keyType = 'string';11}
1<?php23class Flight extends Model4{5 /**6 * The data type of the primary key ID.7 *8 * @var string9 */10 protected $keyType = 'string';11}
"Composite" Primary Keys
Eloquent requires each model to have at least one uniquely identifying "ID" that can serve as its primary key. "Composite" primary keys are not supported by Eloquent models. However, you are free to add additional multi-column, unique indexes to your database tables in addition to the table's uniquely identifying primary key.
UUID and ULID Keys
Instead of using auto-incrementing integers as your Eloquent model's primary keys, you may choose to use UUIDs instead. UUIDs are universally unique alpha-numeric identifiers that are 36 characters long.
If you would like a model to use a UUID key instead of an auto-incrementing integer key, you may use the Illuminate\Database\Eloquent\Concerns\HasUuids
trait on the model. Of course, you should ensure that the model has a UUID equivalent primary key column:
1use Illuminate\Database\Eloquent\Concerns\HasUuids;2use Illuminate\Database\Eloquent\Model;34class Article extends Model5{6 use HasUuids;78 // ...9}1011$article = Article::create(['title' => 'Traveling to Europe']);1213$article->id; // "8f8e8478-9035-4d23-b9a7-62f4d2612ce5"
1use Illuminate\Database\Eloquent\Concerns\HasUuids;2use Illuminate\Database\Eloquent\Model;34class Article extends Model5{6 use HasUuids;78 // ...9}1011$article = Article::create(['title' => 'Traveling to Europe']);1213$article->id; // "8f8e8478-9035-4d23-b9a7-62f4d2612ce5"
By default, The HasUuids
trait will generate "ordered" UUIDs for your models. These UUIDs are more efficient for indexed database storage because they can be sorted lexicographically.
You can override the UUID generation process for a given model by defining a newUniqueId
method on the model. In addition, you may specify which columns should receive UUIDs by defining a uniqueIds
method on the model:
1use Ramsey\Uuid\Uuid;23/**4 * Generate a new UUID for the model.5 */6public function newUniqueId(): string7{8 return (string) Uuid::uuid4();9}1011/**12 * Get the columns that should receive a unique identifier.13 *14 * @return array<int, string>15 */16public function uniqueIds(): array17{18 return ['id', 'discount_code'];19}
1use Ramsey\Uuid\Uuid;23/**4 * Generate a new UUID for the model.5 */6public function newUniqueId(): string7{8 return (string) Uuid::uuid4();9}1011/**12 * Get the columns that should receive a unique identifier.13 *14 * @return array<int, string>15 */16public function uniqueIds(): array17{18 return ['id', 'discount_code'];19}
If you wish, you may choose to utilize "ULIDs" instead of UUIDs. ULIDs are similar to UUIDs; however, they are only 26 characters in length. Like ordered UUIDs, ULIDs are lexicographically sortable for efficient database indexing. To utilize ULIDs, you should use the Illuminate\Database\Eloquent\Concerns\HasUlids
trait on your model. You should also ensure that the model has a ULID equivalent primary key column:
1use Illuminate\Database\Eloquent\Concerns\HasUlids;2use Illuminate\Database\Eloquent\Model;34class Article extends Model5{6 use HasUlids;78 // ...9}1011$article = Article::create(['title' => 'Traveling to Asia']);1213$article->id; // "01gd4d3tgrrfqeda94gdbtdk5c"
1use Illuminate\Database\Eloquent\Concerns\HasUlids;2use Illuminate\Database\Eloquent\Model;34class Article extends Model5{6 use HasUlids;78 // ...9}1011$article = Article::create(['title' => 'Traveling to Asia']);1213$article->id; // "01gd4d3tgrrfqeda94gdbtdk5c"
Timestamps
By default, Eloquent expects created_at
and updated_at
columns to exist on your model's corresponding database table. Eloquent will automatically set these column's values when models are created or updated. If you do not want these columns to be automatically managed by Eloquent, you should define a $timestamps
property on your model with a value of false
:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * Indicates if the model should be timestamped.11 *12 * @var bool13 */14 public $timestamps = false;15}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * Indicates if the model should be timestamped.11 *12 * @var bool13 */14 public $timestamps = false;15}
If you need to customize the format of your model's timestamps, set the $dateFormat
property on your model. This property determines how date attributes are stored in the database as well as their format when the model is serialized to an array or JSON:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The storage format of the model's date columns.11 *12 * @var string13 */14 protected $dateFormat = 'U';15}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The storage format of the model's date columns.11 *12 * @var string13 */14 protected $dateFormat = 'U';15}
If you need to customize the names of the columns used to store the timestamps, you may define CREATED_AT
and UPDATED_AT
constants on your model:
1<?php23class Flight extends Model4{5 const CREATED_AT = 'creation_date';6 const UPDATED_AT = 'updated_date';7}
1<?php23class Flight extends Model4{5 const CREATED_AT = 'creation_date';6 const UPDATED_AT = 'updated_date';7}
If you would like to perform model operations without the model having its updated_at
timestamp modified, you may operate on the model within a closure given to the withoutTimestamps
method:
1Model::withoutTimestamps(fn () => $post->increment(['reads']));
1Model::withoutTimestamps(fn () => $post->increment(['reads']));
Database Connections
By default, all Eloquent models will use the default database connection that is configured for your application. If you would like to specify a different connection that should be used when interacting with a particular model, you should define a $connection
property on the model:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The database connection that should be used by the model.11 *12 * @var string13 */14 protected $connection = 'sqlite';15}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The database connection that should be used by the model.11 *12 * @var string13 */14 protected $connection = 'sqlite';15}
Default Attribute Values
By default, a newly instantiated model instance will not contain any attribute values. If you would like to define the default values for some of your model's attributes, you may define an $attributes
property on your model. Attribute values placed in the $attributes
array should be in their raw, "storable" format as if they were just read from the database:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The model's default values for attributes.11 *12 * @var array13 */14 protected $attributes = [15 'options' => '[]',16 'delayed' => false,17 ];18}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The model's default values for attributes.11 *12 * @var array13 */14 protected $attributes = [15 'options' => '[]',16 'delayed' => false,17 ];18}
Configuring Eloquent Strictness
Laravel offers several methods that allow you to configure Eloquent's behavior and "strictness" in a variety of situations.
First, the preventLazyLoading
method accepts an optional boolean argument that indicates if lazy loading should be prevented. For example, you may wish to only disable lazy loading in non-production environments so that your production environment will continue to function normally even if a lazy loaded relationship is accidentally present in production code. Typically, this method should be invoked in the boot
method of your application's AppServiceProvider
:
1use Illuminate\Database\Eloquent\Model;23/**4 * Bootstrap any application services.5 */6public function boot(): void7{8 Model::preventLazyLoading(! $this->app->isProduction());9}
1use Illuminate\Database\Eloquent\Model;23/**4 * Bootstrap any application services.5 */6public function boot(): void7{8 Model::preventLazyLoading(! $this->app->isProduction());9}
Also, you may instruct Laravel to throw an exception when attempting to fill an unfillable attribute by invoking the preventSilentlyDiscardingAttributes
method. This can help prevent unexpected errors during local development when attempting to set an attribute that has not been added to the model's fillable
array:
1Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
1Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
Retrieving Models
Once you have created a model and its associated database table, you are ready to start retrieving data from your database. You can think of each Eloquent model as a powerful query builder allowing you to fluently query the database table associated with the model. The model's all
method will retrieve all of the records from the model's associated database table:
1use App\Models\Flight;23foreach (Flight::all() as $flight) {4 echo $flight->name;5}
1use App\Models\Flight;23foreach (Flight::all() as $flight) {4 echo $flight->name;5}
Building Queries
The Eloquent all
method will return all of the results in the model's table. However, since each Eloquent model serves as a query builder, you may add additional constraints to queries and then invoke the get
method to retrieve the results:
1$flights = Flight::where('active', 1)2 ->orderBy('name')3 ->take(10)4 ->get();
1$flights = Flight::where('active', 1)2 ->orderBy('name')3 ->take(10)4 ->get();
Since Eloquent models are query builders, you should review all of the methods provided by Laravel's query builder. You may use any of these methods when writing your Eloquent queries.
Refreshing Models
If you already have an instance of an Eloquent model that was retrieved from the database, you can "refresh" the model using the fresh
and refresh
methods. The fresh
method will re-retrieve the model from the database. The existing model instance will not be affected:
1$flight = Flight::where('number', 'FR 900')->first();23$freshFlight = $flight->fresh();
1$flight = Flight::where('number', 'FR 900')->first();23$freshFlight = $flight->fresh();
The refresh
method will re-hydrate the existing model using fresh data from the database. In addition, all of its loaded relationships will be refreshed as well:
1$flight = Flight::where('number', 'FR 900')->first();23$flight->number = 'FR 456';45$flight->refresh();67$flight->number; // "FR 900"
1$flight = Flight::where('number', 'FR 900')->first();23$flight->number = 'FR 456';45$flight->refresh();67$flight->number; // "FR 900"
Collections
As we have seen, Eloquent methods like all
and get
retrieve multiple records from the database. However, these methods don't return a plain PHP array. Instead, an instance of Illuminate\Database\Eloquent\Collection
is returned.
The Eloquent Collection
class extends Laravel's base Illuminate\Support\Collection
class, which provides a variety of helpful methods for interacting with data collections. For example, the reject
method may be used to remove models from a collection based on the results of an invoked closure:
1$flights = Flight::where('destination', 'Paris')->get();23$flights = $flights->reject(function (Flight $flight) {4 return $flight->cancelled;5});
1$flights = Flight::where('destination', 'Paris')->get();23$flights = $flights->reject(function (Flight $flight) {4 return $flight->cancelled;5});
In addition to the methods provided by Laravel's base collection class, the Eloquent collection class provides a few extra methods that are specifically intended for interacting with collections of Eloquent models.
Since all of Laravel's collections implement PHP's iterable interfaces, you may loop over collections as if they were an array:
1foreach ($flights as $flight) {2 echo $flight->name;3}
1foreach ($flights as $flight) {2 echo $flight->name;3}
Chunking Results
Your application may run out of memory if you attempt to load tens of thousands of Eloquent records via the all
or get
methods. Instead of using these methods, the chunk
method may be used to process large numbers of models more efficiently.
The chunk
method will retrieve a subset of Eloquent models, passing them to a closure for processing. Since only the current chunk of Eloquent models is retrieved at a time, the chunk
method will provide significantly reduced memory usage when working with a large number of models:
1use App\Models\Flight;2use Illuminate\Database\Eloquent\Collection;34Flight::chunk(200, function (Collection $flights) {5 foreach ($flights as $flight) {6 // ...7 }8});
1use App\Models\Flight;2use Illuminate\Database\Eloquent\Collection;34Flight::chunk(200, function (Collection $flights) {5 foreach ($flights as $flight) {6 // ...7 }8});
The first argument passed to the chunk
method is the number of records you wish to receive per "chunk". The closure passed as the second argument will be invoked for each chunk that is retrieved from the database. A database query will be executed to retrieve each chunk of records passed to the closure.
If you are filtering the results of the chunk
method based on a column that you will also be updating while iterating over the results, you should use the chunkById
method. Using the chunk
method in these scenarios could lead to unexpected and inconsistent results. Internally, the chunkById
method will always retrieve models with an id
column greater than the last model in the previous chunk:
1Flight::where('departed', true)2 ->chunkById(200, function (Collection $flights) {3 $flights->each->update(['departed' => false]);4 }, $column = 'id');
1Flight::where('departed', true)2 ->chunkById(200, function (Collection $flights) {3 $flights->each->update(['departed' => false]);4 }, $column = 'id');
Chunking Using Lazy Collections
The lazy
method works similarly to the chunk
method in the sense that, behind the scenes, it executes the query in chunks. However, instead of passing each chunk directly into a callback as is, the lazy
method returns a flattened LazyCollection
of Eloquent models, which lets you interact with the results as a single stream:
1use App\Models\Flight;23foreach (Flight::lazy() as $flight) {4 // ...5}
1use App\Models\Flight;23foreach (Flight::lazy() as $flight) {4 // ...5}
If you are filtering the results of the lazy
method based on a column that you will also be updating while iterating over the results, you should use the lazyById
method. Internally, the lazyById
method will always retrieve models with an id
column greater than the last model in the previous chunk:
1Flight::where('departed', true)2 ->lazyById(200, $column = 'id')3 ->each->update(['departed' => false]);
1Flight::where('departed', true)2 ->lazyById(200, $column = 'id')3 ->each->update(['departed' => false]);
You may filter the results based on the descending order of the id
using the lazyByIdDesc
method.
Cursors
Similar to the lazy
method, the cursor
method may be used to significantly reduce your application's memory consumption when iterating through tens of thousands of Eloquent model records.
The cursor
method will only execute a single database query; however, the individual Eloquent models will not be hydrated until they are actually iterated over. Therefore, only one Eloquent model is kept in memory at any given time while iterating over the cursor.
Since the cursor
method only ever holds a single Eloquent model in memory at a time, it cannot eager load relationships. If you need to eager load relationships, consider using the lazy
method instead.
Internally, the cursor
method uses PHP generators to implement this functionality:
1use App\Models\Flight;23foreach (Flight::where('destination', 'Zurich')->cursor() as $flight) {4 // ...5}
1use App\Models\Flight;23foreach (Flight::where('destination', 'Zurich')->cursor() as $flight) {4 // ...5}
The cursor
returns an Illuminate\Support\LazyCollection
instance. Lazy collections allow you to use many of the collection methods available on typical Laravel collections while only loading a single model into memory at a time:
1use App\Models\User;23$users = User::cursor()->filter(function (User $user) {4 return $user->id > 500;5});67foreach ($users as $user) {8 echo $user->id;9}
1use App\Models\User;23$users = User::cursor()->filter(function (User $user) {4 return $user->id > 500;5});67foreach ($users as $user) {8 echo $user->id;9}
Although the cursor
method uses far less memory than a regular query (by only holding a single Eloquent model in memory at a time), it will still eventually run out of memory. This is due to PHP's PDO driver internally caching all raw query results in its buffer. If you're dealing with a very large number of Eloquent records, consider using the lazy
method instead.
Advanced Subqueries
Subquery Selects
Eloquent also offers advanced subquery support, which allows you to pull information from related tables in a single query. For example, let's imagine that we have a table of flight destinations
and a table of flights
to destinations. The flights
table contains an arrived_at
column which indicates when the flight arrived at the destination.
Using the subquery functionality available to the query builder's select
and addSelect
methods, we can select all of the destinations
and the name of the flight that most recently arrived at that destination using a single query:
1use App\Models\Destination;2use App\Models\Flight;34return Destination::addSelect(['last_flight' => Flight::select('name')5 ->whereColumn('destination_id', 'destinations.id')6 ->orderByDesc('arrived_at')7 ->limit(1)8])->get();
1use App\Models\Destination;2use App\Models\Flight;34return Destination::addSelect(['last_flight' => Flight::select('name')5 ->whereColumn('destination_id', 'destinations.id')6 ->orderByDesc('arrived_at')7 ->limit(1)8])->get();
Subquery Ordering
In addition, the query builder's orderBy
function supports subqueries. Continuing to use our flight example, we may use this functionality to sort all destinations based on when the last flight arrived at that destination. Again, this may be done while executing a single database query:
1return Destination::orderByDesc(2 Flight::select('arrived_at')3 ->whereColumn('destination_id', 'destinations.id')4 ->orderByDesc('arrived_at')5 ->limit(1)6)->get();
1return Destination::orderByDesc(2 Flight::select('arrived_at')3 ->whereColumn('destination_id', 'destinations.id')4 ->orderByDesc('arrived_at')5 ->limit(1)6)->get();
Retrieving Single Models / Aggregates
In addition to retrieving all of the records matching a given query, you may also retrieve single records using the find
, first
, or firstWhere
methods. Instead of returning a collection of models, these methods return a single model instance:
1use App\Models\Flight;23// Retrieve a model by its primary key...4$flight = Flight::find(1);56// Retrieve the first model matching the query constraints...7$flight = Flight::where('active', 1)->first();89// Alternative to retrieving the first model matching the query constraints...10$flight = Flight::firstWhere('active', 1);
1use App\Models\Flight;23// Retrieve a model by its primary key...4$flight = Flight::find(1);56// Retrieve the first model matching the query constraints...7$flight = Flight::where('active', 1)->first();89// Alternative to retrieving the first model matching the query constraints...10$flight = Flight::firstWhere('active', 1);
Sometimes you may wish to perform some other action if no results are found. The findOr
and firstOr
methods will return a single model instance or, if no results are found, execute the given closure. The value returned by the closure will be considered the result of the method:
1$flight = Flight::findOr(1, function () {2 // ...3});45$flight = Flight::where('legs', '>', 3)->firstOr(function () {6 // ...7});
1$flight = Flight::findOr(1, function () {2 // ...3});45$flight = Flight::where('legs', '>', 3)->firstOr(function () {6 // ...7});
Not Found Exceptions
Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The findOrFail
and firstOrFail
methods will retrieve the first result of the query; however, if no result is found, an Illuminate\Database\Eloquent\ModelNotFoundException
will be thrown:
1$flight = Flight::findOrFail(1);23$flight = Flight::where('legs', '>', 3)->firstOrFail();
1$flight = Flight::findOrFail(1);23$flight = Flight::where('legs', '>', 3)->firstOrFail();
If the ModelNotFoundException
is not caught, a 404 HTTP response is automatically sent back to the client:
1use App\Models\Flight;23Route::get('/api/flights/{id}', function (string $id) {4 return Flight::findOrFail($id);5});
1use App\Models\Flight;23Route::get('/api/flights/{id}', function (string $id) {4 return Flight::findOrFail($id);5});
Retrieving or Creating Models
The firstOrCreate
method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the attributes resulting from merging the first array argument with the optional second array argument:
The firstOrNew
method, like firstOrCreate
, will attempt to locate a record in the database matching the given attributes. However, if a model is not found, a new model instance will be returned. Note that the model returned by firstOrNew
has not yet been persisted to the database. You will need to manually call the save
method to persist it:
1use App\Models\Flight;23// Retrieve flight by name or create it if it doesn't exist...4$flight = Flight::firstOrCreate([5 'name' => 'London to Paris'6]);78// Retrieve flight by name or create it with the name, delayed, and arrival_time attributes...9$flight = Flight::firstOrCreate(10 ['name' => 'London to Paris'],11 ['delayed' => 1, 'arrival_time' => '11:30']12);1314// Retrieve flight by name or instantiate a new Flight instance...15$flight = Flight::firstOrNew([16 'name' => 'London to Paris'17]);1819// Retrieve flight by name or instantiate with the name, delayed, and arrival_time attributes...20$flight = Flight::firstOrNew(21 ['name' => 'Tokyo to Sydney'],22 ['delayed' => 1, 'arrival_time' => '11:30']23);
1use App\Models\Flight;23// Retrieve flight by name or create it if it doesn't exist...4$flight = Flight::firstOrCreate([5 'name' => 'London to Paris'6]);78// Retrieve flight by name or create it with the name, delayed, and arrival_time attributes...9$flight = Flight::firstOrCreate(10 ['name' => 'London to Paris'],11 ['delayed' => 1, 'arrival_time' => '11:30']12);1314// Retrieve flight by name or instantiate a new Flight instance...15$flight = Flight::firstOrNew([16 'name' => 'London to Paris'17]);1819// Retrieve flight by name or instantiate with the name, delayed, and arrival_time attributes...20$flight = Flight::firstOrNew(21 ['name' => 'Tokyo to Sydney'],22 ['delayed' => 1, 'arrival_time' => '11:30']23);
Retrieving Aggregates
When interacting with Eloquent models, you may also use the count
, sum
, max
, and other aggregate methods provided by the Laravel query builder. As you might expect, these methods return a scalar value instead of an Eloquent model instance:
1$count = Flight::where('active', 1)->count();23$max = Flight::where('active', 1)->max('price');
1$count = Flight::where('active', 1)->count();23$max = Flight::where('active', 1)->max('price');
Inserting and Updating Models
Inserts
Of course, when using Eloquent, we don't only need to retrieve models from the database. We also need to insert new records. Thankfully, Eloquent makes it simple. To insert a new record into the database, you should instantiate a new model instance and set attributes on the model. Then, call the save
method on the model instance:
1<?php23namespace App\Http\Controllers;45use App\Http\Controllers\Controller;6use App\Models\Flight;7use Illuminate\Http\RedirectResponse;8use Illuminate\Http\Request;910class FlightController extends Controller11{12 /**13 * Store a new flight in the database.14 */15 public function store(Request $request): RedirectResponse16 {17 // Validate the request...1819 $flight = new Flight;2021 $flight->name = $request->name;2223 $flight->save();2425 return redirect('/flights');26 }27}
1<?php23namespace App\Http\Controllers;45use App\Http\Controllers\Controller;6use App\Models\Flight;7use Illuminate\Http\RedirectResponse;8use Illuminate\Http\Request;910class FlightController extends Controller11{12 /**13 * Store a new flight in the database.14 */15 public function store(Request $request): RedirectResponse16 {17 // Validate the request...1819 $flight = new Flight;2021 $flight->name = $request->name;2223 $flight->save();2425 return redirect('/flights');26 }27}
In this example, we assign the name
field from the incoming HTTP request to the name
attribute of the App\Models\Flight
model instance. When we call the save
method, a record will be inserted into the database. The model's created_at
and updated_at
timestamps will automatically be set when the save
method is called, so there is no need to set them manually.
Alternatively, you may use the create
method to "save" a new model using a single PHP statement. The inserted model instance will be returned to you by the create
method:
1use App\Models\Flight;23$flight = Flight::create([4 'name' => 'London to Paris',5]);
1use App\Models\Flight;23$flight = Flight::create([4 'name' => 'London to Paris',5]);
However, before using the create
method, you will need to specify either a fillable
or guarded
property on your model class. These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default. To learn more about mass assignment, please consult the mass assignment documentation.
Updates
The save
method may also be used to update models that already exist in the database. To update a model, you should retrieve it and set any attributes you wish to update. Then, you should call the model's save
method. Again, the updated_at
timestamp will automatically be updated, so there is no need to manually set its value:
1use App\Models\Flight;23$flight = Flight::find(1);45$flight->name = 'Paris to London';67$flight->save();
1use App\Models\Flight;23$flight = Flight::find(1);45$flight->name = 'Paris to London';67$flight->save();
Mass Updates
Updates can also be performed against models that match a given query. In this example, all flights that are active
and have a destination
of San Diego
will be marked as delayed:
1Flight::where('active', 1)2 ->where('destination', 'San Diego')3 ->update(['delayed' => 1]);
1Flight::where('active', 1)2 ->where('destination', 'San Diego')3 ->update(['delayed' => 1]);
The update
method expects an array of column and value pairs representing the columns that should be updated. The update
method returns the number of affected rows.
When issuing a mass update via Eloquent, the saving
, saved
, updating
, and updated
model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.
Examining Attribute Changes
Eloquent provides the isDirty
, isClean
, and wasChanged
methods to examine the internal state of your model and determine how its attributes have changed from when the model was originally retrieved.
The isDirty
method determines if any of the model's attributes have been changed since the model was retrieved. You may pass a specific attribute name or an array of attributes to the isDirty
method to determine if any of the attributes are "dirty". The isClean
method will determine if an attribute has remained unchanged since the model was retrieved. This method also accepts an optional attribute argument:
1use App\Models\User;23$user = User::create([4 'first_name' => 'Taylor',5 'last_name' => 'Otwell',6 'title' => 'Developer',7]);89$user->title = 'Painter';1011$user->isDirty(); // true12$user->isDirty('title'); // true13$user->isDirty('first_name'); // false14$user->isDirty(['first_name', 'title']); // true1516$user->isClean(); // false17$user->isClean('title'); // false18$user->isClean('first_name'); // true19$user->isClean(['first_name', 'title']); // false2021$user->save();2223$user->isDirty(); // false24$user->isClean(); // true
1use App\Models\User;23$user = User::create([4 'first_name' => 'Taylor',5 'last_name' => 'Otwell',6 'title' => 'Developer',7]);89$user->title = 'Painter';1011$user->isDirty(); // true12$user->isDirty('title'); // true13$user->isDirty('first_name'); // false14$user->isDirty(['first_name', 'title']); // true1516$user->isClean(); // false17$user->isClean('title'); // false18$user->isClean('first_name'); // true19$user->isClean(['first_name', 'title']); // false2021$user->save();2223$user->isDirty(); // false24$user->isClean(); // true
The wasChanged
method determines if any attributes were changed when the model was last saved within the current request cycle. If needed, you may pass an attribute name to see if a particular attribute was changed:
1$user = User::create([2 'first_name' => 'Taylor',3 'last_name' => 'Otwell',4 'title' => 'Developer',5]);67$user->title = 'Painter';89$user->save();1011$user->wasChanged(); // true12$user->wasChanged('title'); // true13$user->wasChanged(['title', 'slug']); // true14$user->wasChanged('first_name'); // false15$user->wasChanged(['first_name', 'title']); // true
1$user = User::create([2 'first_name' => 'Taylor',3 'last_name' => 'Otwell',4 'title' => 'Developer',5]);67$user->title = 'Painter';89$user->save();1011$user->wasChanged(); // true12$user->wasChanged('title'); // true13$user->wasChanged(['title', 'slug']); // true14$user->wasChanged('first_name'); // false15$user->wasChanged(['first_name', 'title']); // true
The getOriginal
method returns an array containing the original attributes of the model regardless of any changes to the model since it was retrieved. If needed, you may pass a specific attribute name to get the original value of a particular attribute:
1$user = User::find(1);23$user->name; // John56$user->name = "Jack";7$user->name; // Jack89$user->getOriginal('name'); // John10$user->getOriginal(); // Array of original attributes...
1$user = User::find(1);23$user->name; // John56$user->name = "Jack";7$user->name; // Jack89$user->getOriginal('name'); // John10$user->getOriginal(); // Array of original attributes...
Mass Assignment
You may use the create
method to "save" a new model using a single PHP statement. The inserted model instance will be returned to you by the method:
1use App\Models\Flight;23$flight = Flight::create([4 'name' => 'London to Paris',5]);
1use App\Models\Flight;23$flight = Flight::create([4 'name' => 'London to Paris',5]);
However, before using the create
method, you will need to specify either a fillable
or guarded
property on your model class. These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default.
A mass assignment vulnerability occurs when a user passes an unexpected HTTP request field and that field changes a column in your database that you did not expect. For example, a malicious user might send an is_admin
parameter through an HTTP request, which is then passed to your model's create
method, allowing the user to escalate themselves to an administrator.
So, to get started, you should define which model attributes you want to make mass assignable. You may do this using the $fillable
property on the model. For example, let's make the name
attribute of our Flight
model mass assignable:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The attributes that are mass assignable.11 *12 * @var array13 */14 protected $fillable = ['name'];15}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class Flight extends Model8{9 /**10 * The attributes that are mass assignable.11 *12 * @var array13 */14 protected $fillable = ['name'];15}
Once you have specified which attributes are mass assignable, you may use the create
method to insert a new record in the database. The create
method returns the newly created model instance:
1$flight = Flight::create(['name' => 'London to Paris']);
1$flight = Flight::create(['name' => 'London to Paris']);
If you already have a model instance, you may use the fill
method to populate it with an array of attributes:
1$flight->fill(['name' => 'Amsterdam to Frankfurt']);
1$flight->fill(['name' => 'Amsterdam to Frankfurt']);
Mass Assignment and JSON Columns
When assigning JSON columns, each column's mass assignable key must be specified in your model's $fillable
array. For security, Laravel does not support updating nested JSON attributes when using the guarded
property:
1/**2 * The attributes that are mass assignable.3 *4 * @var array5 */6protected $fillable = [7 'options->enabled',8];
1/**2 * The attributes that are mass assignable.3 *4 * @var array5 */6protected $fillable = [7 'options->enabled',8];
Allowing Mass Assignment
If you would like to make all of your attributes mass assignable, you may define your model's $guarded
property as an empty array. If you choose to unguard your model, you should take special care to always hand-craft the arrays passed to Eloquent's fill
, create
, and update
methods:
1/**2 * The attributes that aren't mass assignable.3 *4 * @var array5 */6protected $guarded = [];
1/**2 * The attributes that aren't mass assignable.3 *4 * @var array5 */6protected $guarded = [];
Mass Assignment Exceptions
By default, attributes that are not included in the $fillable
array are silently discarded when performing mass-assignment operations. In production, this is expected behavior; however, during local development it can lead to confusion as to why model changes are not taking effect.
If you wish, you may instruct Laravel to throw an exception when attempting to fill an unfillable attribute by invoking the preventSilentlyDiscardingAttributes
method. Typically, this method should be invoked within the boot
method of one of your application's service providers:
1use Illuminate\Database\Eloquent\Model;23/**4 * Bootstrap any application services.5 */6public function boot(): void7{8 Model::preventSilentlyDiscardingAttributes($this->app->isLocal());9}
1use Illuminate\Database\Eloquent\Model;23/**4 * Bootstrap any application services.5 */6public function boot(): void7{8 Model::preventSilentlyDiscardingAttributes($this->app->isLocal());9}
Upserts
Occasionally, you may need to update an existing model or create a new model if no matching model exists. Like the firstOrCreate
method, the updateOrCreate
method persists the model, so there's no need to manually call the save
method.
In the example below, if a flight exists with a departure
location of Oakland
and a destination
location of San Diego
, its price
and discounted
columns will be updated. If no such flight exists, a new flight will be created which has the attributes resulting from merging the first argument array with the second argument array:
1$flight = Flight::updateOrCreate(2 ['departure' => 'Oakland', 'destination' => 'San Diego'],3 ['price' => 99, 'discounted' => 1]4);
1$flight = Flight::updateOrCreate(2 ['departure' => 'Oakland', 'destination' => 'San Diego'],3 ['price' => 99, 'discounted' => 1]4);
If you would like to perform multiple "upserts" in a single query, then you should use the upsert
method instead. The method's first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the associated table. The method's third and final argument is an array of the columns that should be updated if a matching record already exists in the database. The upsert
method will automatically set the created_at
and updated_at
timestamps if timestamps are enabled on the model:
1Flight::upsert([2 ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],3 ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]4], ['departure', 'destination'], ['price']);
1Flight::upsert([2 ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],3 ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]4], ['departure', 'destination'], ['price']);
All databases except SQL Server require the columns in the second argument of the upsert
method to have a "primary" or "unique" index. In addition, the MySQL database driver ignores the second argument of the upsert
method and always uses the "primary" and "unique" indexes of the table to detect existing records.
Deleting Models
To delete a model, you may call the delete
method on the model instance:
1use App\Models\Flight;23$flight = Flight::find(1);45$flight->delete();
1use App\Models\Flight;23$flight = Flight::find(1);45$flight->delete();
You may call the truncate
method to delete all of the model's associated database records. The truncate
operation will also reset any auto-incrementing IDs on the model's associated table:
1Flight::truncate();
1Flight::truncate();
Deleting an Existing Model by its Primary Key
In the example above, we are retrieving the model from the database before calling the delete
method. However, if you know the primary key of the model, you may delete the model without explicitly retrieving it by calling the destroy
method. In addition to accepting the single primary key, the destroy
method will accept multiple primary keys, an array of primary keys, or a collection of primary keys:
1Flight::destroy(1);23Flight::destroy(1, 2, 3);45Flight::destroy([1, 2, 3]);67Flight::destroy(collect([1, 2, 3]));
1Flight::destroy(1);23Flight::destroy(1, 2, 3);45Flight::destroy([1, 2, 3]);67Flight::destroy(collect([1, 2, 3]));
The destroy
method loads each model individually and calls the delete
method so that the deleting
and deleted
events are properly dispatched for each model.
Deleting Models Using Queries
Of course, you may build an Eloquent query to delete all models matching your query's criteria. In this example, we will delete all flights that are marked as inactive. Like mass updates, mass deletes will not dispatch model events for the models that are deleted:
1$deleted = Flight::where('active', 0)->delete();
1$deleted = Flight::where('active', 0)->delete();
When executing a mass delete statement via Eloquent, the deleting
and deleted
model events will not be dispatched for the deleted models. This is because the models are never actually retrieved when executing the delete statement.
Soft Deleting
In addition to actually removing records from your database, Eloquent can also "soft delete" models. When models are soft deleted, they are not actually removed from your database. Instead, a deleted_at
attribute is set on the model indicating the date and time at which the model was "deleted". To enable soft deletes for a model, add the Illuminate\Database\Eloquent\SoftDeletes
trait to the model:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;6use Illuminate\Database\Eloquent\SoftDeletes;78class Flight extends Model9{10 use SoftDeletes;11}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;6use Illuminate\Database\Eloquent\SoftDeletes;78class Flight extends Model9{10 use SoftDeletes;11}
The SoftDeletes
trait will automatically cast the deleted_at
attribute to a DateTime
/ Carbon
instance for you.
You should also add the deleted_at
column to your database table. The Laravel schema builder contains a helper method to create this column:
1use Illuminate\Database\Schema\Blueprint;2use Illuminate\Support\Facades\Schema;34Schema::table('flights', function (Blueprint $table) {5 $table->softDeletes();6});78Schema::table('flights', function (Blueprint $table) {9 $table->dropSoftDeletes();10});
1use Illuminate\Database\Schema\Blueprint;2use Illuminate\Support\Facades\Schema;34Schema::table('flights', function (Blueprint $table) {5 $table->softDeletes();6});78Schema::table('flights', function (Blueprint $table) {9 $table->dropSoftDeletes();10});
Now, when you call the delete
method on the model, the deleted_at
column will be set to the current date and time. However, the model's database record will be left in the table. When querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results.
To determine if a given model instance has been soft deleted, you may use the trashed
method:
1if ($flight->trashed()) {2 // ...3}
1if ($flight->trashed()) {2 // ...3}
Restoring Soft Deleted Models
Sometimes you may wish to "un-delete" a soft deleted model. To restore a soft deleted model, you may call the restore
method on a model instance. The restore
method will set the model's deleted_at
column to null
:
1$flight->restore();
1$flight->restore();
You may also use the restore
method in a query to restore multiple models. Again, like other "mass" operations, this will not dispatch any model events for the models that are restored:
1Flight::withTrashed()2 ->where('airline_id', 1)3 ->restore();
1Flight::withTrashed()2 ->where('airline_id', 1)3 ->restore();
The restore
method may also be used when building relationship queries:
1$flight->history()->restore();
1$flight->history()->restore();
Permanently Deleting Models
Sometimes you may need to truly remove a model from your database. You may use the forceDelete
method to permanently remove a soft deleted model from the database table:
1$flight->forceDelete();
1$flight->forceDelete();
You may also use the forceDelete
method when building Eloquent relationship queries:
1$flight->history()->forceDelete();
1$flight->history()->forceDelete();
Querying Soft Deleted Models
Including Soft Deleted Models
As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to be included in a query's results by calling the withTrashed
method on the query:
1use App\Models\Flight;23$flights = Flight::withTrashed()4 ->where('account_id', 1)5 ->get();
1use App\Models\Flight;23$flights = Flight::withTrashed()4 ->where('account_id', 1)5 ->get();
The withTrashed
method may also be called when building a relationship query:
1$flight->history()->withTrashed()->get();
1$flight->history()->withTrashed()->get();
Retrieving Only Soft Deleted Models
The onlyTrashed
method will retrieve only soft deleted models:
1$flights = Flight::onlyTrashed()2 ->where('airline_id', 1)3 ->get();
1$flights = Flight::onlyTrashed()2 ->where('airline_id', 1)3 ->get();
Pruning Models
Sometimes you may want to periodically delete models that are no longer needed. To accomplish this, you may add the Illuminate\Database\Eloquent\Prunable
or Illuminate\Database\Eloquent\MassPrunable
trait to the models you would like to periodically prune. After adding one of the traits to the model, implement a prunable
method which returns an Eloquent query builder that resolves the models that are no longer needed:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;7use Illuminate\Database\Eloquent\Prunable;89class Flight extends Model10{11 use Prunable;1213 /**14 * Get the prunable model query.15 */16 public function prunable(): Builder17 {18 return static::where('created_at', '<=', now()->subMonth());19 }20}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;7use Illuminate\Database\Eloquent\Prunable;89class Flight extends Model10{11 use Prunable;1213 /**14 * Get the prunable model query.15 */16 public function prunable(): Builder17 {18 return static::where('created_at', '<=', now()->subMonth());19 }20}
When marking models as Prunable
, you may also define a pruning
method on the model. This method will be called before the model is deleted. This method can be useful for deleting any additional resources associated with the model, such as stored files, before the model is permanently removed from the database:
1/**2 * Prepare the model for pruning.3 */4protected function pruning(): void5{6 // ...7}
1/**2 * Prepare the model for pruning.3 */4protected function pruning(): void5{6 // ...7}
After configuring your prunable model, you should schedule the model:prune
Artisan command in your application's App\Console\Kernel
class. You are free to choose the appropriate interval at which this command should be run:
1/**2 * Define the application's command schedule.3 */4protected function schedule(Schedule $schedule): void5{6 $schedule->command('model:prune')->daily();7}
1/**2 * Define the application's command schedule.3 */4protected function schedule(Schedule $schedule): void5{6 $schedule->command('model:prune')->daily();7}
Behind the scenes, the model:prune
command will automatically detect "Prunable" models within your application's app/Models
directory. If your models are in a different location, you may use the --model
option to specify the model class names:
1$schedule->command('model:prune', [2 '--model' => [Address::class, Flight::class],3])->daily();
1$schedule->command('model:prune', [2 '--model' => [Address::class, Flight::class],3])->daily();
If you wish to exclude certain models from being pruned while pruning all other detected models, you may use the --except
option:
1$schedule->command('model:prune', [2 '--except' => [Address::class, Flight::class],3])->daily();
1$schedule->command('model:prune', [2 '--except' => [Address::class, Flight::class],3])->daily();
You may test your prunable
query by executing the model:prune
command with the --pretend
option. When pretending, the model:prune
command will simply report how many records would be pruned if the command were to actually run:
1php artisan model:prune --pretend
1php artisan model:prune --pretend
Soft deleting models will be permanently deleted (forceDelete
) if they match the prunable query.
Mass Pruning
When models are marked with the Illuminate\Database\Eloquent\MassPrunable
trait, models are deleted from the database using mass-deletion queries. Therefore, the pruning
method will not be invoked, nor will the deleting
and deleted
model events be dispatched. This is because the models are never actually retrieved before deletion, thus making the pruning process much more efficient:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;7use Illuminate\Database\Eloquent\MassPrunable;89class Flight extends Model10{11 use MassPrunable;1213 /**14 * Get the prunable model query.15 */16 public function prunable(): Builder17 {18 return static::where('created_at', '<=', now()->subMonth());19 }20}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;7use Illuminate\Database\Eloquent\MassPrunable;89class Flight extends Model10{11 use MassPrunable;1213 /**14 * Get the prunable model query.15 */16 public function prunable(): Builder17 {18 return static::where('created_at', '<=', now()->subMonth());19 }20}
Replicating Models
You may create an unsaved copy of an existing model instance using the replicate
method. This method is particularly useful when you have model instances that share many of the same attributes:
1use App\Models\Address;23$shipping = Address::create([4 'type' => 'shipping',5 'line_1' => '123 Example Street',6 'city' => 'Victorville',7 'state' => 'CA',8 'postcode' => '90001',9]);1011$billing = $shipping->replicate()->fill([12 'type' => 'billing'13]);1415$billing->save();
1use App\Models\Address;23$shipping = Address::create([4 'type' => 'shipping',5 'line_1' => '123 Example Street',6 'city' => 'Victorville',7 'state' => 'CA',8 'postcode' => '90001',9]);1011$billing = $shipping->replicate()->fill([12 'type' => 'billing'13]);1415$billing->save();
To exclude one or more attributes from being replicated to the new model, you may pass an array to the replicate
method:
1$flight = Flight::create([2 'destination' => 'LAX',3 'origin' => 'LHR',4 'last_flown' => '2020-03-04 11:00:00',5 'last_pilot_id' => 747,6]);78$flight = $flight->replicate([9 'last_flown',10 'last_pilot_id'11]);
1$flight = Flight::create([2 'destination' => 'LAX',3 'origin' => 'LHR',4 'last_flown' => '2020-03-04 11:00:00',5 'last_pilot_id' => 747,6]);78$flight = $flight->replicate([9 'last_flown',10 'last_pilot_id'11]);
Query Scopes
Global Scopes
Global scopes allow you to add constraints to all queries for a given model. Laravel's own soft delete functionality utilizes global scopes to only retrieve "non-deleted" models from the database. Writing your own global scopes can provide a convenient, easy way to make sure every query for a given model receives certain constraints.
Generating Scopes
To generate a new global scope, you may invoke the make:scope
Artisan command, which will place the generated scope in your application's app/Models/Scopes
directory:
1php artisan make:scope AncientScope
1php artisan make:scope AncientScope
Writing Global Scopes
Writing a global scope is simple. First, use the make:scope
command to generate a class that implements the Illuminate\Database\Eloquent\Scope
interface. The Scope
interface requires you to implement one method: apply
. The apply
method may add where
constraints or other types of clauses to the query as needed:
1<?php23namespace App\Models\Scopes;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;7use Illuminate\Database\Eloquent\Scope;89class AncientScope implements Scope10{11 /**12 * Apply the scope to a given Eloquent query builder.13 */14 public function apply(Builder $builder, Model $model): void15 {16 $builder->where('created_at', '<', now()->subYears(2000));17 }18}
1<?php23namespace App\Models\Scopes;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;7use Illuminate\Database\Eloquent\Scope;89class AncientScope implements Scope10{11 /**12 * Apply the scope to a given Eloquent query builder.13 */14 public function apply(Builder $builder, Model $model): void15 {16 $builder->where('created_at', '<', now()->subYears(2000));17 }18}
If your global scope is adding columns to the select clause of the query, you should use the addSelect
method instead of select
. This will prevent the unintentional replacement of the query's existing select clause.
Applying Global Scopes
To assign a global scope to a model, you may simply place the ScopedBy
attribute on the model:
1<?php23namespace App\Models;45use App\Models\Scopes\AncientScope;6use Illuminate\Database\Eloquent\Attributes\ScopedBy;78#[ScopedBy([AncientScope::class])]9class User extends Model10{11 //12}
1<?php23namespace App\Models;45use App\Models\Scopes\AncientScope;6use Illuminate\Database\Eloquent\Attributes\ScopedBy;78#[ScopedBy([AncientScope::class])]9class User extends Model10{11 //12}
Or, you may manually register the global scope by overriding the model's booted
method and invoke the model's addGlobalScope
method. The addGlobalScope
method accepts an instance of your scope as its only argument:
1<?php23namespace App\Models;45use App\Models\Scopes\AncientScope;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * The "booted" method of the model.12 */13 protected static function booted(): void14 {15 static::addGlobalScope(new AncientScope);16 }17}
1<?php23namespace App\Models;45use App\Models\Scopes\AncientScope;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * The "booted" method of the model.12 */13 protected static function booted(): void14 {15 static::addGlobalScope(new AncientScope);16 }17}
After adding the scope in the example above to the App\Models\User
model, a call to the User::all()
method will execute the following SQL query:
1select * from `users` where `created_at` < 0021-02-18 00:00:00
1select * from `users` where `created_at` < 0021-02-18 00:00:00
Anonymous Global Scopes
Eloquent also allows you to define global scopes using closures, which is particularly useful for simple scopes that do not warrant a separate class of their own. When defining a global scope using a closure, you should provide a scope name of your own choosing as the first argument to the addGlobalScope
method:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * The "booted" method of the model.12 */13 protected static function booted(): void14 {15 static::addGlobalScope('ancient', function (Builder $builder) {16 $builder->where('created_at', '<', now()->subYears(2000));17 });18 }19}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * The "booted" method of the model.12 */13 protected static function booted(): void14 {15 static::addGlobalScope('ancient', function (Builder $builder) {16 $builder->where('created_at', '<', now()->subYears(2000));17 });18 }19}
Removing Global Scopes
If you would like to remove a global scope for a given query, you may use the withoutGlobalScope
method. This method accepts the class name of the global scope as its only argument:
1User::withoutGlobalScope(AncientScope::class)->get();
1User::withoutGlobalScope(AncientScope::class)->get();
Or, if you defined the global scope using a closure, you should pass the string name that you assigned to the global scope:
1User::withoutGlobalScope('ancient')->get();
1User::withoutGlobalScope('ancient')->get();
If you would like to remove several or even all of the query's global scopes, you may use the withoutGlobalScopes
method:
1// Remove all of the global scopes...2User::withoutGlobalScopes()->get();34// Remove some of the global scopes...5User::withoutGlobalScopes([6 FirstScope::class, SecondScope::class7])->get();
1// Remove all of the global scopes...2User::withoutGlobalScopes()->get();34// Remove some of the global scopes...5User::withoutGlobalScopes([6 FirstScope::class, SecondScope::class7])->get();
Local Scopes
Local scopes allow you to define common sets of query constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, prefix an Eloquent model method with scope
.
Scopes should always return the same query builder instance or void
:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * Scope a query to only include popular users.12 */13 public function scopePopular(Builder $query): void14 {15 $query->where('votes', '>', 100);16 }1718 /**19 * Scope a query to only include active users.20 */21 public function scopeActive(Builder $query): void22 {23 $query->where('active', 1);24 }25}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * Scope a query to only include popular users.12 */13 public function scopePopular(Builder $query): void14 {15 $query->where('votes', '>', 100);16 }1718 /**19 * Scope a query to only include active users.20 */21 public function scopeActive(Builder $query): void22 {23 $query->where('active', 1);24 }25}
Utilizing a Local Scope
Once the scope has been defined, you may call the scope methods when querying the model. However, you should not include the scope
prefix when calling the method. You can even chain calls to various scopes:
1use App\Models\User;23$users = User::popular()->active()->orderBy('created_at')->get();
1use App\Models\User;23$users = User::popular()->active()->orderBy('created_at')->get();
Combining multiple Eloquent model scopes via an or
query operator may require the use of closures to achieve the correct logical grouping:
1$users = User::popular()->orWhere(function (Builder $query) {2 $query->active();3})->get();
1$users = User::popular()->orWhere(function (Builder $query) {2 $query->active();3})->get();
However, since this can be cumbersome, Laravel provides a "higher order" orWhere
method that allows you to fluently chain scopes together without the use of closures:
1$users = User::popular()->orWhere->active()->get();
1$users = User::popular()->orWhere->active()->get();
Dynamic Scopes
Sometimes you may wish to define a scope that accepts parameters. To get started, just add your additional parameters to your scope method's signature. Scope parameters should be defined after the $query
parameter:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * Scope a query to only include users of a given type.12 */13 public function scopeOfType(Builder $query, string $type): void14 {15 $query->where('type', $type);16 }17}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Builder;6use Illuminate\Database\Eloquent\Model;78class User extends Model9{10 /**11 * Scope a query to only include users of a given type.12 */13 public function scopeOfType(Builder $query, string $type): void14 {15 $query->where('type', $type);16 }17}
Once the expected arguments have been added to your scope method's signature, you may pass the arguments when calling the scope:
1$users = User::ofType('admin')->get();
1$users = User::ofType('admin')->get();
Comparing Models
Sometimes you may need to determine if two models are the "same" or not. The is
and isNot
methods may be used to quickly verify two models have the same primary key, table, and database connection or not:
1if ($post->is($anotherPost)) {2 // ...3}45if ($post->isNot($anotherPost)) {6 // ...7}
1if ($post->is($anotherPost)) {2 // ...3}45if ($post->isNot($anotherPost)) {6 // ...7}
The is
and isNot
methods are also available when using the belongsTo
, hasOne
, morphTo
, and morphOne
relationships. This method is particularly helpful when you would like to compare a related model without issuing a query to retrieve that model:
1if ($post->author()->is($user)) {2 // ...3}
1if ($post->author()->is($user)) {2 // ...3}
Events
Want to broadcast your Eloquent events directly to your client-side application? Check out Laravel's model event broadcasting.
Eloquent models dispatch several events, allowing you to hook into the following moments in a model's lifecycle: retrieved
, creating
, created
, updating
, updated
, saving
, saved
, deleting
, deleted
, trashed
, forceDeleting
, forceDeleted
, restoring
, restored
, and replicating
.
The retrieved
event will dispatch when an existing model is retrieved from the database. When a new model is saved for the first time, the creating
and created
events will dispatch. The updating
/ updated
events will dispatch when an existing model is modified and the save
method is called. The saving
/ saved
events will dispatch when a model is created or updated - even if the model's attributes have not been changed. Event names ending with -ing
are dispatched before any changes to the model are persisted, while events ending with -ed
are dispatched after the changes to the model are persisted.
To start listening to model events, define a $dispatchesEvents
property on your Eloquent model. This property maps various points of the Eloquent model's lifecycle to your own event classes. Each model event class should expect to receive an instance of the affected model via its constructor:
1<?php23namespace App\Models;45use App\Events\UserDeleted;6use App\Events\UserSaved;7use Illuminate\Foundation\Auth\User as Authenticatable;8use Illuminate\Notifications\Notifiable;910class User extends Authenticatable11{12 use Notifiable;1314 /**15 * The event map for the model.16 *17 * @var array18 */19 protected $dispatchesEvents = [20 'saved' => UserSaved::class,21 'deleted' => UserDeleted::class,22 ];23}
1<?php23namespace App\Models;45use App\Events\UserDeleted;6use App\Events\UserSaved;7use Illuminate\Foundation\Auth\User as Authenticatable;8use Illuminate\Notifications\Notifiable;910class User extends Authenticatable11{12 use Notifiable;1314 /**15 * The event map for the model.16 *17 * @var array18 */19 protected $dispatchesEvents = [20 'saved' => UserSaved::class,21 'deleted' => UserDeleted::class,22 ];23}
After defining and mapping your Eloquent events, you may use event listeners to handle the events.
When issuing a mass update or delete query via Eloquent, the saved
, updated
, deleting
, and deleted
model events will not be dispatched for the affected models. This is because the models are never actually retrieved when performing mass updates or deletes.
Using Closures
Instead of using custom event classes, you may register closures that execute when various model events are dispatched. Typically, you should register these closures in the booted
method of your model:
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class User extends Model8{9 /**10 * The "booted" method of the model.11 */12 protected static function booted(): void13 {14 static::created(function (User $user) {15 // ...16 });17 }18}
1<?php23namespace App\Models;45use Illuminate\Database\Eloquent\Model;67class User extends Model8{9 /**10 * The "booted" method of the model.11 */12 protected static function booted(): void13 {14 static::created(function (User $user) {15 // ...16 });17 }18}
If needed, you may utilize queueable anonymous event listeners when registering model events. This will instruct Laravel to execute the model event listener in the background using your application's queue:
1use function Illuminate\Events\queueable;23static::created(queueable(function (User $user) {4 // ...5}));
1use function Illuminate\Events\queueable;23static::created(queueable(function (User $user) {4 // ...5}));
Observers
Defining Observers
If you are listening for many events on a given model, you may use observers to group all of your listeners into a single class. Observer classes have method names which reflect the Eloquent events you wish to listen for. Each of these methods receives the affected model as their only argument. The make:observer
Artisan command is the easiest way to create a new observer class:
1php artisan make:observer UserObserver --model=User
1php artisan make:observer UserObserver --model=User
This command will place the new observer in your app/Observers
directory. If this directory does not exist, Artisan will create it for you. Your fresh observer will look like the following:
1<?php23namespace App\Observers;45use App\Models\User;67class UserObserver8{9 /**10 * Handle the User "created" event.11 */12 public function created(User $user): void13 {14 // ...15 }1617 /**18 * Handle the User "updated" event.19 */20 public function updated(User $user): void21 {22 // ...23 }2425 /**26 * Handle the User "deleted" event.27 */28 public function deleted(User $user): void29 {30 // ...31 }3233 /**34 * Handle the User "restored" event.35 */36 public function restored(User $user): void37 {38 // ...39 }4041 /**42 * Handle the User "forceDeleted" event.43 */44 public function forceDeleted(User $user): void45 {46 // ...47 }48}
1<?php23namespace App\Observers;45use App\Models\User;67class UserObserver8{9 /**10 * Handle the User "created" event.11 */12 public function created(User $user): void13 {14 // ...15 }1617 /**18 * Handle the User "updated" event.19 */20 public function updated(User $user): void21 {22 // ...23 }2425 /**26 * Handle the User "deleted" event.27 */28 public function deleted(User $user): void29 {30 // ...31 }3233 /**34 * Handle the User "restored" event.35 */36 public function restored(User $user): void37 {38 // ...39 }4041 /**42 * Handle the User "forceDeleted" event.43 */44 public function forceDeleted(User $user): void45 {46 // ...47 }48}
To register an observer, you may place the ObservedBy
attribute on the corresponding model:
1use App\Observers\UserObserver;2use Illuminate\Database\Eloquent\Attributes\ObservedBy;34#[ObservedBy([UserObserver::class])]5class User extends Authenticatable6{7 //8}
1use App\Observers\UserObserver;2use Illuminate\Database\Eloquent\Attributes\ObservedBy;34#[ObservedBy([UserObserver::class])]5class User extends Authenticatable6{7 //8}
Or, you may manually register an observer by calling the observe
method on the model you wish to observe. You may register observers in the boot
method of your application's App\Providers\EventServiceProvider
service provider:
1use App\Models\User;2use App\Observers\UserObserver;34/**5 * Register any events for your application.6 */7public function boot(): void8{9 User::observe(UserObserver::class);10}
1use App\Models\User;2use App\Observers\UserObserver;34/**5 * Register any events for your application.6 */7public function boot(): void8{9 User::observe(UserObserver::class);10}
There are additional events an observer can listen to, such as saving
and retrieved
. These events are described within the events documentation.
Observers and Database Transactions
When models are being created within a database transaction, you may want to instruct an observer to only execute its event handlers after the database transaction is committed. You may accomplish this by implementing the ShouldHandleEventsAfterCommit
interface on your observer. If a database transaction is not in progress, the event handlers will execute immediately:
1<?php23namespace App\Observers;45use App\Models\User;6use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;78class UserObserver implements ShouldHandleEventsAfterCommit9{10 /**11 * Handle the User "created" event.12 */13 public function created(User $user): void14 {15 // ...16 }17}
1<?php23namespace App\Observers;45use App\Models\User;6use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;78class UserObserver implements ShouldHandleEventsAfterCommit9{10 /**11 * Handle the User "created" event.12 */13 public function created(User $user): void14 {15 // ...16 }17}
Muting Events
You may occasionally need to temporarily "mute" all events fired by a model. You may achieve this using the withoutEvents
method. The withoutEvents
method accepts a closure as its only argument. Any code executed within this closure will not dispatch model events, and any value returned by the closure will be returned by the withoutEvents
method:
1use App\Models\User;23$user = User::withoutEvents(function () {4 User::findOrFail(1)->delete();56 return User::find(2);7});
1use App\Models\User;23$user = User::withoutEvents(function () {4 User::findOrFail(1)->delete();56 return User::find(2);7});
Saving a Single Model Without Events
Sometimes you may wish to "save" a given model without dispatching any events. You may accomplish this using the saveQuietly
method:
1$user = User::findOrFail(1);23$user->name = 'Victoria Faith';45$user->saveQuietly();
1$user = User::findOrFail(1);23$user->name = 'Victoria Faith';45$user->saveQuietly();
You may also "update", "delete", "soft delete", "restore", and "replicate" a given model without dispatching any events:
1$user->deleteQuietly();2$user->forceDeleteQuietly();3$user->restoreQuietly();
1$user->deleteQuietly();2$user->forceDeleteQuietly();3$user->restoreQuietly();