How To Add Custom Attribute in Laravel Model Tutorial

Reading Time: 4 minutes
1,390 Views

Inside this article we will see the concept i.e How To Add Custom Attribute in Laravel Model Tutorial. Article contains the classified information about Adding Custom Laravel Eloquent Model Attributes.

If you are working with models, sometimes you need to concat two or more scalar values into a single attribute. So here, comes the concept of creating custom model attributes. In laravel either we can define custom attribute or we can append to Eloquent Model Object.

In this example, we have user table with first_name and last_name columns. we will create full_name custom attribute and access it with user object.

Read More: How to Use DB Raw Query in Laravel Explained Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Naming Convention for Custom Attribute Function name

Syntax: get{ColumnName}Attribute

Examples:

  • getFullNameAttribute() – full_name
  • getAgeAttribute() – age
  • getCompleteAddressAttribute – complete_address

Let’s see about custom attributes in model.

Example #1: Define Custom Attribute in Laravel Model

For this tutorial, we are considering a User model and let’s see how to create a custom attribute.

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
  
class User extends Authenticatable
{
    use HasFactory, Notifiable;
  
    ....
  
    /**
     * Determine  full name of user
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    public function getFullNameAttribute()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
}

Access Attribute in Application

To access custom attribute in application –

$full_name = User::find(1)->full_name;

Read More: Laravel How To Get Min Value of Column Tutorial

Example:

first_name: Sanjay and last_name: Kumar then full_name: Sanjay Kumar

Example #2: Model Define Attribute with Append Property

Again, we are considering a User model and let’s see how to append a custom attribute. Using $appends property, you will get always “full_name” with user model object.

<?php
  
namespace App\Models;
   
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
   
class User extends Authenticatable
{
    use HasFactory, Notifiable;
  
    ....
  
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['full_name'];
  
    /**
     * Determine  full name of user
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute
     */
    public function getFullNameAttribute()
    {
        return $this->first_name . ' ' . $this->last_name;
    }
}

Access Attribute in Application

To access custom attribute in application –

$user = User::find(1);

Output will be,

(
    [id] => 1
    [first_name] => Sanjay 
    [last_name] => Kumar
    [email] => sanjay_test@gmail.com
    [created_at] => 2023-01-23T12:58:18.000000Z
    [updated_at] => 2023-01-23T12:58:18.000000Z
    [full_name] => Sanjay Kumar
)

We hope this article helped you to learn How To Add Custom Attribute in Laravel Model Tutorial in a very detailed way.

Read More: Laravel How To Get Max Value of Column Tutorial

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.