Laravel Eloquent - Tips & Tricks - InterWorks

Laravel Eloquent – Tips & Tricks

At this point, we all know what Laravel is all about. But, if you want to find out more information on it, check the official Laravel website. This blog will focus on Laravel Eloquent. 

Eloquent ORM is included with Laravel and provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Given the fact that there are a lot of features that come out-of-the-box with Laravel and it is hard to know them all, in this article, we are going to give some examples of the ones that are less known.

1.The Exists Property

The exists property is used to show whether the object exists in the database or not. When you create a new model instance the exists property will be set to false. Once your model is saved or retrieved from the database the exists property will be set to true.

2. 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 since their original loading.

The isDirty() method determines if any attributes have been changed since the model was loaded. You may pass a specific attribute name to determine if a particular attribute is dirty. The isClean() method is the opposite of isDirty() and also accepts an optional attribute argument:

The wasChanged() method determines if any attributes were changed when the model was last saved within the current request cycle. You may also pass an attribute name to see if a particular attribute was changed:

3. The push() method

The push() method saves the model and all of its relationships.

If we call the save method only the user’s age will be saved and not the address. In order for that to happen, we need to call the push method. The push method saves the original model and all of its relationships.

4. Model boot() method

There is a magical place called boot() in an Eloquent model where you can override the default behavior. One of the most popular examples is setting some field value at the moment of creating the model object:

5. Find multiple entries

The find() method is quite common, but you’d be quite surprised how few people know that it can accept multiple IDs as an array:

6. Eloquent when(), say goodbye to if-else

Instead of using if and else conditionals, Eloquent provides a convenient method when(). For example, when we are filtering some table based on multiple conditions:

7. Raw query methods

This is a lot more popular and widely known but it is still a very useful one. Whenever we need to add some raw SQL query we use the following functions:

8. Chunk() method for big datasets

When we are working with big datasets instead of getting all the records for some table we can get a certain amount with the chunk() method like this:

9. Transform brackets into an Eloquent query

So, imagine we have the following SQL query:

How do we convert it to Eloquent?

10. Use hasMany() to create many

If we have a hasMany() relationship, we can use saveMany() to save multiple “child” entries from our “parent” object, all in one sentence:

Conclusion

Eloquent has some really good functionalities, and the tips and tricks that we elaborated above are not broadly known or documented properly. We hope you will find them useful and implement them in your daily routine when working with Laravel and Eloquent. For any additional information please feel free to contact us and connect on social media.