8 interesting functions of Laravel Eloquent (ORM)

Iman Borumand Zadeh
4 min readJun 29, 2022

--

Laravel eloquent

In this article, we want to learn more about 8 Laravel Eloquent functions and introduce them

One of the concerns of developers is creating security in queries, creating complex queries, Speeding up development when working with databases, Easier switching between different databases, etc. when using the database in their applications.

Laravel uses a feature called Eloquent to remove this problem. Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. Eloquent facilitates the task of interacting with database tables, providing an object-oriented approach to inserting, updating, and deleting database records, while also providing a streamlined interface for executing complex SQL queries.

In this article, we want to learn more about 8 Laravel Eloquent functions and introduce them. Fasten your seat belt and get ready to go!

1- Replicate

How do you do this if you want to copy or simulate a database record? To do this, Laravel has introduced a function called Replicate.

Using this function you can take a function and apply the changes you want to it and then save it.

Suppose you have a store product and you want to make a copy of it and change its title and save and publish it.

Laravel Replicate ORM

This replicate method is really helpful for quickly cloning or duplicating a database record.

2- One of Many

Suppose there is a User model that has a one-to-many relationship with the Post model. Now you want to select the first and last posts of the user.

You can use latestOfMany and oldestOfMany that Laravel provides to do this.

Laravel ORM One of Many

The latestPost function returns the last post sent by the user. The oldestPost function also returns the oldest post sent by the user based on the model primary key. But you may want to retrieve a record with special conditions. For example, to retrieve the user post with the most visits, you can do the following:

Laravel ORM One of Many

notes: You can also apply a filter to the result using a closure function.

3- reject

This function is used to delete models based on the desired condition.

For example, in the following example, we want to remove products that are priced above 200 from the collection

Laravel reject ORM

4- isDirty

isDirty method determines if any model attribute has been changed when it retrieves.

We can also pass one or more attributes to this method to check if a change has been made.
The isDirty() function is used after calling a model and returns true or false values that have changed and have not changed.

For example, we have a Post model that we want to check for changes.

Laravel isDirty

5- truncate

You can use the truncate function to delete all data related to the model in the database.

Post::truncate();

The above command will delete all the table data related to the Post model!

6- is & isNot

You may need to check if the two models are the same. The is and isNot functions can do this for you

Laravel ORM is & isNot

7- withCount (Count on relationship)

You can return the number of rows in a relationship using the withCount function.
Suppose you have a user model that has a large number of messages. You can return the number of messages of a user with the command withCount.
Take a look at the following example:

withCount Laravel ORM

8- withWhereHas()

the method that simplifies cases where you have to repeat code used to both filter with whereHas and select the same record via with() (This function is available from Laravel version 9.16.0 onwards)

If you enjoyed this article, share your joy with me by clicking the clap button :)

--

--