Hello Laravel Friends,

In this blog, we are learning how to add a custom attribute in the Laravel model. In this blog, we are going through the example of custom attributes.

What are the Use Cases of Custom Attributes in Laravel?

When we need,

  • some extra information / some calculation logic in the model
  • we need to process the model data
  • get relational data with the model object

In all the above cases, we can use a custom attribute in Laravel.

Let’s take one example of it.

Example of Custom Attribute in Laravel

Here we have one fresh Laravel project. Also, we have a User model with first_name, last_name, email, and password. Here we need to display the user’s full name in our view. So need to create a custom attribute for it. We are creating the full_name attribute in the User model.

Now, your app/Models/User.php file looks like the below.

To access your custom attribute using the model object.


Magecomp Laravel

Suppose you need a full_name attribute with a User model object. You can achieve that using the $appends property of the model class. Please add the following code to your User model.

Now access your custom attribute using the User model object.



Hence, using the above method, you can easily Add a Custom Attribute in Laravel Model. For any doubts or queries, freely leave a comment, and I will be prompt to answer them. For any customization requirements of your Laravel project, Hire Laravel Developer. Share the tutorial with your other Laravel friends, and stay in touch with us to learn more about Laravel.

Happy Coding!

Click to rate this post!
[Total: 7 Average: 4.4]