Laravel Eloquent ORM

In this tutorial you will learn about the Laravel Eloquent ORM and its application with practical example.

What is Eloquent ORM?

One of the most important feature of the laravel framework is that, it comes with built in ORM (Object Relation Mapping) called Eloquent ORM.
Eloquent ORM refer to an advanced implementation of the PHP Active Record Pattern, which makes it very easy to interact with application database. Eloquent ORM is the very powerful yet very expressive ORM, which allow us to work with the database objects and relationships using much eloquent and expressive syntax. In Laravel, each database table is mapped into corresponding eloquent model and each of the eloquent model object include various methods for retrieving and updating the database.

Eloquent ORM CRUD Operations

Eloquent ORM makes it incredibly easy to perform CRUD (Create, Read, Update, Delete) operations on Laravel Model, since we have already covered how to create Laravel Model in previous article, so we will create new “Post” model using following artisan command –

Let’s open the newly generated model file, at this point your model should look like as following –

just edit your model as following –

Eloquent ORM Create

To insert a record in database table we need to create a model instance, set or assign attributes values and then call save() method as following –

In Laravel, we have two other methods to insert a record in database table(firstOrCreate and firstOrNew). The firstOrCreate method will try to retrieve the record matching to the column value pair. If the record not found in the table, a record will be inserted with the given column value pair. similarly, firstOrNew method method will try to retrieve the record matching to the column value pair. If the record not found in the table, a new model instance will be created with the given column value pair then you need to call save method to save the record.

Eloquent ORM Read

Retrieving all records

In Laravel, all records can be retrieved from a table using all() method as following –

and you can loop through resultset for individual records as following –

Using Where Clause

Where Clause with Additional Constraints

Retrieving Single Records

In Laravel, single record can be retrieved using find() and first() method as following –

Record Not Found Exceptions

In Laravel, the findOrFail() and firstOrFail() methods retrieves the first result of the query and if record is not found an exception is thrown.

Retrieving Aggregates

In Laravel, you can use aggregate functions like count, min, max, avg, sum etc. as following –

Eloquent ORM Update

To update a record you have to retrieve it first, set values for attributes that you want to update, and then call the save method.

Eloquent ORM Delete

To delete a record in laravel, simply call the delete method as following –

You can delete a record directly using destroy method instead of retrieving it first, to do so simply call destroy method passing single or multiple primary keys as following –

You can also call delete method on query result set as following –

Mass assignment

Laravel model allow us to assign multiple attributes values while creating model instance using the mass assignment. In Laravel model using the $fillable property we can declare array of attributes that is mass-assignable. For security concern Laravel supports a $guarded property which declare array of attributes that you do not want to be mass assignable.

Example:-

Soft Deleting

In Laravel, a soft deleted record is not actually removed from your database instead a deleted_at attribute is set for that record. Records with non-null deleted_at value are considered as soft deleted. To enable soft deletes for a laravel model, simply use the Illuminate\Database\Eloquent\SoftDeletes trait on the model and add the deleted_at column to your $dates property.

Example:-

Including Soft Deleted Models

By default soft deleted records are not included in the query result-set. However, if you want to include soft deleted records in query result, it can be done using withTrashed method in query.

Example:-

Retrieving Only Soft Deleted Models

If you want to retrieve soft deleted records only, it can be done using onlyTrashed method as following –

Example:-

Determine if Soft Deleted

If you want to check if a record has been soft deleted or not, use the trashed method as following –

Restoring Soft Deleted Models

Soft deleted record can also be recovered using restore method as following –

Permanently Deleting Models

If you want to permanently remove a soft deleted record from the database, it can be done using forceDelete method as following –

Example 1:-

Example 2:-

 

In this tutorial we have learn about the Laravel Eloquent ORM and its application with practical example. I hope you will like this tutorial.