Version
Eloquent: Getting Started
- Introduction
- Generating Model Classes
- Eloquent Model Conventions
- Table Names
- Primary Keys
- UUID & ULID Keys
- Timestamps
- Database Connections
- Default Attribute Values
- Retrieving Models
- Collections
- Chunking Results
- Chunk Using Lazy Collections
- Cursors
- Advanced Subqueries
- Retrieving Single Models /
Aggregates
- Retrieving Or Creating Models
- Retrieving Aggregates
- Inserting & Updating Models
- Inserts
- Updates
- Mass Assignment
- Upserts
- Deleting Models
- Soft Deleting
- Querying Soft Deleted Models
- Pruning Models
- Replicating Models
- Query Scopes
- Global Scopes
- Local Scopes
- Comparing Models
- Events
- Using Closures
- Observers
- Muting 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.
Note
Before getting started, be sure to configure a database connection in your application'sconfig/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 the 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:
php 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:
php 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:
# Generate a model and a FlightFactory class...
php artisan make:model Flight --factory
php artisan make:model Flight -f
# Generate a model and a FlightSeeder class...
php artisan make:model Flight --seed
php artisan make:model Flight -s
# Generate a model and a FlightController class...
php artisan make:model Flight --controller
php artisan make:model Flight -c
# Generate a model, FlightController resource class, and form request classes...
php artisan make:model Flight --controller --resource --requests
php artisan make:model Flight -crR
# Generate a model and a FlightPolicy class...
php artisan make:model Flight --policy
# Generate a model and a migration, factory, seeder, and controller...
php artisan make:model Flight -mfsc
# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...
php artisan make:model Flight --all
# Generate a pivot model...
php artisan make:model Member --pivot
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:
php 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: