---
title: "How to Add Custom Attributes in Laravel Model?"
url: "https://magecomp.com/blog/add-custom-attributes-in-laravel-model/"
date: "2023-02-15T14:48:21+00:00"
modified: "2025-01-06T10:01:20+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "How To"
  - "Laravel"
word_count: 486
reading_time: "3 min read"
summary: "Hello Laravel Friends,"
description: "When you need some extra information or logic in the model you can achieve it by adding a custom attribute in the Laravel model. Learn how"
keywords: "How To, Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Redirect to Previous Page URL in Magento 2"
    url: "https://magecomp.com/blog/redirect-to-previous-page-url-magento-2/"
  - title: "How to Define Custom Form Validation in Magento"
    url: "https://magecomp.com/blog/define-custom-form-validation-in-magento/"
  - title: "Magento 2: Hide Other Shipping Methods if Free Shipping is Available"
    url: "https://magecomp.com/blog/magento-2-hide-shipping-methods-free-shipping-available/"
---

# How to Add Custom Attributes in Laravel Model?

_Published: February 15, 2023_  
_Author: Bharat Desai_  

![How to Add Custom Attributes in Laravel Model](https://magecomp.com/blog/wp-content/uploads/2023/02/How-to-Add-Custom-Attributes-in-Laravel-Model-1024x512.webp)

**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.
[![Laravel Development Services](https://magecomp.com/blog/wp-content/uploads/2024/12/Laravel-Development-Services-4.webp)](https://magecomp.com/services/laravel-development-services/)

## 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.
```
<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

// your custom attribute
    public function getFullNameAttribute()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
}
```

To access your custom attribute using the model object.
```
return User::find(1)->full_name;
```

**Output:**

Magecomp Laravel
[![Hire laravel Developer](https://magecomp.com/blog/wp-content/uploads/2024/12/Hire-Laravel-Expert-Now-3.webp)](https://magecomp.com/services/hire-laravel-developer/)

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.
```
<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;

class User extends Authenticatable
{
    …

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'full_name'
    ];

    public function getFullNameAttribute()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
}
```

Now access your custom attribute using the User model object.
```
return User::find(1);
```

**Output:**

```
{
    first_name: 'Magecomp',
    Last_name:  'Laravel',
    email : 'johndoe@gmail.com',
    email_verified_at : null,
    created_at: '2023-02-15 06:10:20',
    updated_at:  '2023-02-15 06:10:20',
    full_name: 'Magecomp Laravel'
}
```

## Conclusion:
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**](https://magecomp.com/hire-laravel-developer.html). Share the tutorial with your other Laravel friends, and stay in touch with us to learn more about Laravel.
[![Contact Us](https://magecomp.com/blog/wp-content/uploads/2024/12/Contact-Us-3.webp)](https://magecomp.com/contact)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/add-custom-attributes-in-laravel-model/](https://magecomp.com/blog/add-custom-attributes-in-laravel-model/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-10 06:46:34 UTC_  
