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 add re-captcha v3 on all Elementor forms using coding?

 Add re-captcha v3 on all Elementor forms using coding add_action('wp_footer',function(){     ?> <script src="https://www...