Thursday, 2 January 2020

laravel eloquent mutators

<?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:
$user = App\User::find(1);

$firstName = $user->first_name
Carbon

Eloquent mutators:
        // $a = new Carbon(
        //     '2019-12-30 18:48:39.463000', 'UTC'
        // );

        // $a->setTimezone('America/Los_Angeles');
        // echo var_dump($a);
        // echo var_dump($a->format('Y-m-d H:i:s'));
        // die();
;

No comments:

Post a Comment