ItSolutionStuff.com

How to Add Custom Attribute in Laravel Model?

By Hardik Savani • November 5, 2023
Laravel

Hi,

I will explain step by step tutorial how to add attribute in laravel model. you'll learn laravel append attribute to model. It's a simple example of laravel append value to model. This article goes in detailed on custom attribute in laravel model. Let's see below example laravel append attribute to model.

If you want to set custom attributes in laravel model, then I will help you. I will give you two simple examples of append attributes to model and access it. 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.

Let's see one by one example:

Example 1: Laravel Model Define Attribute

app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable

{

use HasApiTokens, 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:

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

Output:

Hardik Savani

Example 2: Laravel Model Define Attribute with Append Property

Using $appends property, you will get always "full_name" with user model object.

app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable

{

use HasApiTokens, 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:

$user = User::find(1);

Output:

Array

(

[id] => 1

[first_name] => Hardik

[last_name] => Savani

[email] => aatmaninfotech@gmail.com

[created_at] => 2022-05-23T12:58:18.000000Z

[updated_at] => 2022-05-23T12:58:18.000000Z

[full_name] => Hardik Savani

)

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Get Array of Ids from Eloquent Models in Laravel

Read Now →

How to Set Default Value in Laravel Model?

Read Now →

How to Select Specific Columns in Laravel Eloquent Model?

Read Now →

How to Get All Models in Laravel?

Read Now →

How to Get Columns Names from Model in Laravel?

Read Now →

How to Create Model in Laravel using Command?

Read Now →

Laravel 9 Enum Model Attribute Casting Example

Read Now →

How to Create Custom Model Events in Laravel?

Read Now →

Laravel Replicate Model with Relationships Example

Read Now →

Laravel Model Events Tutorial

Read Now →

Laravel Improve Speed Performance using Model Caching Tutorial

Read Now →

How to disable model timestamps in Laravel?

Read Now →

How to Get Table Name from Model in Laravel?

Read Now →