Make the most of the Laravel Model

Tram Ho

When working with the Eloquent Model in Laravel, I found some interesting things that could make our code shorter, easier.

In this article, I will share 7 tips that anyone using the Laravel framework should know for optimizing Models.

First, let’s create a Model

When creating a Model via the command line, we can specify the folder where the Model is to be created. Just add the name of the folder before the Model name.

The above command will create a Model in the app / Models directory. This saves us time moving manually.

1. The Casts property

The $ cast property provides a way to convert the property to a certain data type.

The is_published property will be converted to a boolean when accessed, even if it is stored as integer in the database.

There are many types that we can convert properties to such as date, datetime .

One error that I often see with is the date, datetime properties formatted in Blade templates, like this:

In some Blade templates, we can see this format happening multiple times on the same variable (iteration of code). This can be made simpler through the $ cast property .

The published_at property will always be returned in Ymd format and we don’t need to do any formatting work anymore.

2. Visibility

There are some properties that should not be included in the retrieved data, for example the password property. And this is the case where we can use the $ hidden property .

The $ hidden property is like a blacklist for the property. And in addition, we can also use the $ visible property which is considered a whitelist for the property.

When using the $ visible property in the model, properties that are not set are automatically hidden. It works like $ fillable and $ guarded .

3. Accessors

Occasionally, we need to combine multiple attributes into one (“fake” attribute). This can be done using accessors .

Let’s say you have a User model with properties first_name and last_name . To display the full name we can do the following:

This is what most programmers use. But in Laravel it is discouraged to use it, and use accessors instead.

Accessor is a method defined in the Model with the following syntax:

The accessor for the above example would look like this:

And to access the fullname value we use the following syntax:

4. Mutators

In contrast to Accessor , Mutators allow us to reformat property values ​​before saving them to the database. It has the following syntax:

The above example would apply the ucfirst () function to last_name and store the results in $ attributes :

5. Appending values

When a Model has accessors and relations , by default they won’t be added to the array and the Model’s JSON. To do this, we have to add accessors and relations to the $ appends property of the model. For example with the full_name property:

Note: Accessors are added to the $ append property using the snack case, not the camel case.

Assuming the User model has a 1 – n relationship with the Blog model:

To add a blog to your model, simply add the $ appends attribute:

We can specify additional properties. For example, we want to add the blog id and title properties to the model:

6. Touches

When one model has a relationship BelongsTo or BelongsToMany with another model, like in this case: Comment model belongs to a Blog, when you edit a comment and you want the blog related to this comment to also update from time to time. This can be done by adding the relationship to the $ touches property :

When the Comment model is updated, the Blog model’s updated_at property associated with this comment will be updated automatically.


Above are 7 things that I want to share with you to make the most of the Model’s functionality. Feel free to leave a comment if you have any questions.

Thank you for reading!

Share the news now

Source : Viblo