Updated 18 February 2020
In order to format the Eloquent Attributes Accessors and Mutators are used. Laravel Accessors and Mutators are custom, user defined methods.
Accessors are used to format the attributes when you retrieve them from database.
Whereas, Mutators are used to format the attributes before saving them into the database.
The syntax used for defining an Accessor is very simple, getFooAttribute(). Here Foo is the “studly” cased name of the column you wish to access.
Let’s take an example. In this example, we’ll define an accessor for the first_name attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * Get the user's first name. * * @param string $value * @return string */ public function getFirstNameAttribute($value) { return ucfirst($value); } } |
As you can see, the original value of the column is passed to the accessor, allowing you to manipulate and return the value. To access the value of the accessor, you may access the first_name attribute on a model instance:
1 2 3 |
$user = App\User::find(1); $firstName = $user->first_name; |
Accessor can also be used to return new, computed values from existing attributes:
1 2 3 4 5 6 7 8 9 |
/** * Get the user's full name. * * @return string */ public function getFullNameAttribute() { return "{$this->first_name} {$this->last_name}"; } |
To define a mutator, define a setFooAttribute method on your model where Foo is the “studly” cased name of the column you wish to access. So, again, let’s define a mutator for the first_name attribute. This mutator will be automatically called when we attempt to set the value of the first_name attribute on the model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * Set the user's first name. * * @param string $value * @return void */ public function setFirstNameAttribute($value) { $this->attributes['first_name'] = strtolower($value); } } |
The mutator will receive the value that is being set on the attribute, allowing you to manipulate the value and set the manipulated value on the Eloquent model’s internal $attributes property. So, for example, if we attempt to set the first_name attribute to Sally:
1 2 3 |
$user = App\User::find(1); $user->first_name = 'Sally'; |
In this example, the setFirstNameAttribute function will be called with the value Sally. The mutator will then apply the strtolower function to the name and set its resulting value in the internal $attributes array.
By default, Eloquent will convert the created_at and updated_at columns to instances of Carbon, which extends the PHP DateTime class and provides an assortment of helpful methods. You may add additional date attributes by setting the $dates property of your model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = [ 'seen_at', ]; } |
You may disable the default created_at and updated_at timestamps by setting the public $timestamps property of your model to false.
When a column is considered a date, you may set its value to a UNIX timestamp, date string (y-m-d), date-time string, or a DateTime/ Carbon instance. The date’s value will be correctly converted and stored in your database:
1 2 3 4 5 |
$user = App\User::find(1); $user->deleted_at = now(); $user->save(); |
I really hope that I helped you understand what accessors and mutattors are, and if you don’t already use them I hope you now have an idea for what you can use them in your projects.
Source: Laravel
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.