Attributes Set In Laravel Model

  • 29-05-2022
  • 2896
  • Laravel 9
  • Haresh Chauhan

In this post, You will learn how to set attributes in the laravel model. This built function tools provided by the laravel model using set you can set the attribute according to your determination and using get you will get that attribute base on defined attribute types.

So in this post, you will see how to set an attribute in the laravel model and to set that attribute from that laravel model.

How To Use Set Attribute?

Using setPostAttribute in model it will set post attribute in Json Encode means the post attribute value will convert into json data.

Whenever I create a user with post attribute the model will set the data into JSON format and inset it into database.

public function setPostAttribute($value)
{
    return $this->attributes['post'] = json_encode($value);
}

If I store the post attribute like below, This will store it into JSON data object in database server.

use App\User;

User::create([
        'name' => 'Haresh Chauhan',
        'email' => 'haresh@gmail.com',
        'password' => bcrypt('123465'),
        'post' => [
            'title' => 'This is the post title',
            'body' => 'This is the post body',
        ]
]);

You can see how the post attribute is stored in the database.

{"title":"This is the post title","body":"This is the post body"}

How To Use Get Attribute?

Using getPostAttribute the stord data from database will convert into Json Decode by the function of json_decode you can see in below to get attribute set in the model.

public function getPostAttribute($value)
{
    return json_decode($value);
}

This attribute will return this data from JSON data to the PHP array. Without using the get attribute query will return the data the same as stored in the database. It will not return decode data from the post attribute.

Also, define the get attribute into the model you will see the JSON data like the below example.

// "post" => "{"title":"This is the post title","body":"This is the post body"}"

// get attribute to convert into JSON decode

{#260 ▼
    +"title": "This is the post title"
    +"body": "This is the post body"
}

Let's take look at it full example in the model, How to use both methods in model.

App\User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    // Set Attribute
    public function setPostAttribute($value)
    {
        return $this->attributes['post'] = json_encode($value);
    }

    // Get Attribute
    public function getPostAttribute($value)
    {
        return json_decode($value);
    }
}

You can also define both method in single attribute. Note: This example will work greater than Laravel 9 version.

App\User.php;

<?php
namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    // public function getPostAttribute($value)
    // {
    //     return json_decode($value);
    // }

    // public function setPostAttribute($value)
    // {
    //     return $this->attributes['post'] = json_encode($value);
    // }

    public function post(): Attribute
    {
        return new Arrtibute(
            get: fn($value) => json_encode($value),
            set: fn($value) => json_decode($value),
        );
    }
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :