28 June 2023

How to use Mutators and Accessors in Laravel?

Mutators and Accessors in Laravel     


Set file path in model

public function getFilePathAttribute($file_path)

    {

        if(!empty($file_path))

        {

             return $filepath = URL::to('/') . '/upload/' . $file_path;

/*             return asset('/upoad/' . $file_path); */


        }

    }

Set created time ago in model

protected $appends = ["created_time_ago"];

public function getCreatedTimeAgoAttribute()

    {

        $created_at = $this->attributes['created_at'];

        // Convert the date/time to a Carbon instance

        $carbonCreatedAt = Carbon::parse($created_at);

        // Get the "time ago" format

        $timeAgo = $carbonCreatedAt->diffForHumans();

        // Set the modified value to the column

        return $timeAgo;

    }

Set created time ago in model

public function getRequestedTimeAttribute($requested_time)

    {

        if(!empty($requested_time))

        {

             return date_format($requested_time,"Y-m-d");

        }

    }

No comments:

Post a Comment

How to create youtube videos slider with play and pause option in wordpress?

Create youtube videos slider with play and pause option in wordpress youtube videos slider Use this shortcode:- [punjab_today] function my_...