Laravel 8 Eloquent Accessors and Mutators Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Laravel 8 Eloquent Accessors and Mutators Example

Hello Dev’s
Today now in this blog, i will explain with you eloquent accessors and mutators in laravel 8. Here i will show easy example of eloquent accessors and mutators in laravel 8.

Accessors and mutators are allow you to format Eloquent attribute values. When we retrieve or set them as a model instances. For example, we may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when we want access it on an Eloquent model.

For better know you will need to following example of eloquent accessors and mutators.

Create Eloquent Accessors and Mutators

Now here we can easy to create accessors and mutators in model

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name','last_name', 'email', 'password','birth_date',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
     /**
     * Set the user's first name.
     *
     * @param  string  $value
     * @return void
     */
    public function getFullNameAttribute($value){
         return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
    }
     /**
     * Set the user's first name.
     *
     * @param  string  $value
     * @return void
     */
    public function  getAgeAttribute($value)
    {
         return Carbon::parse($this->birth_date)->age;
    }
    /**
     * Set the user's first name.
     *
     * @param  string  $value
     * @return void
     */
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }
}
Use Accessors and Mutators
<!DOCTYPE html>
<html>
<head>
    <title>Laravel 7/6 Eloquent Accessors and Mutators Example - CodingsPoint.com/</title>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css">
</head>
<body>
    <div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card mt-5">
                <div class="card-header">Laravel 7/6  Eloquent Accessors and Mutators Example - nicesnippets.com</div>
                <div class="card-body">
                   <ul>
                       @foreach($users as $key=>$value)
                            <li>FullName: {{ $value->fullName }}</li>
                            <li>Age: {{ $value->age }}</li>
                       @endforeach
                   </ul>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Read also : How To Set Bcc And Cc Mail Address In Laravel Mail?

I hope it will help you. Also you can follow us on Facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →