How to Add Custom Attribute in Laravel Model?

Introduction

In this blog article, we will explore the steps to add a custom attribute in the Laravel model and enhance the functionality of your application.

Why Custom Attributes?

Custom attributes allow you to add additional properties to your model that are not directly stored in the database. This can be useful when you want to manipulate data or perform calculations based on existing attributes.

Example 1: Define the Attribute

To add a custom attribute, we first need to define it in our model class. Open your model file and add the following code:

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:

Roshan Jha

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:

Output:

Array

(

    [id] => 1

    [first_name] => Roshan

    [last_name] => Jha

    [email] => roshan.cotocus@gmail.com

    [created_at] => 2023-09-21T10:50:11.000000Z

    [updated_at] => 2023-09-21T10:50:11.000000Z

    [full_name] => Roshan Jha

)

I hope it will help you ….!!!!

Related Posts

Best Free Programming Ebooks to Learn Software Development

Learning modern technology can feel overwhelming when tutorials are scattered across dozens of websites. Many software developers, engineering students, and IT professionals struggle to find comprehensive study…

Read More

FinOps Training: Understanding Cost Visibility and Optimization

Introduction Moving workloads to the cloud can simplify infrastructure management, but it can also introduce a new financial challenge. Teams can provision resources whenever they need them,…

Read More

The Architecture of Intelligent DataOps: From Ingestion to Automation

Introduction Modern organizations run on distributed data ecosystems. On any given day, an enterprise environment ingests, transforms, and serves petabytes of records sourced from transactional databases, external…

Read More

Transforming Data Reliability: How DataOps Platforms Drive Proactive Monitoring

Introduction Traditional monitoring focuses almost entirely on infrastructure availability and binary job execution states—whether a server is up or whether a task completed. However, modern distributed environments…

Read More

Navigating Pipeline Risks with Expert DevSecOps Consulting Services

Software delivery moves faster today than at any point in technological history. High-performing engineering organizations push code changes to production multiple times a day using automated deployment…

Read More

DevOps Support Services: Key Practices for Stable Production Environments

Introduction Running modern software infrastructure is an ongoing responsibility. A development team may successfully launch an application, but keeping that application reliable in production requires continuous attention….

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x