1To define a mutator, define a setFooAttribute method on your model where Foo
2 is the "studly" cased name of the column you wish to access. So, again,
3lets define a mutator for the first_name attribute. This mutator will
4be automatically called when we attempt to set the value of the first_name
5attribute on the model:
6
7class User extends Model
8{
9 public function setFirstNameAttribute($value)
10 {
11 $this->attributes['first_name'] = strtolower($value);
12 }
13}
1<?php
2//Array & JSON Casting
3namespace App;
4
5use Illuminate\Database\Eloquent\Model;
6
7class User extends Model
8{
9 /**
10 * The attributes that should be cast.
11 *
12 * @var array
13 */
14 protected $casts = [
15 'options' => 'array',
16 ];
17}
18namespace App;
19
20use Illuminate\Database\Eloquent\Model;
21
22class User extends Model
23{
24 /**
25 * The attributes that should be cast.
26 *
27 * @var array
28 */
29 protected $casts = [
30 'options' => 'array',
31 ];
32}
1protected $casts = [
2 'birthday' => 'date:Y-m-d',
3 'joined_at' => 'datetime:Y-m-d H:00',
4];