date - Where to put this code in laravel? -


i have table in database has 3 columns of type date, stored in y-m-d format. need dates displayed in views d/m/y. model table jobcard.php, every time retrieve instance of jobcard this:

$jobcard -> datereqlatest = carbon::createfromformat('y-m-d', $jobcard -> datereqlatest)->format('d/m/y'); $jobcard -> dateissued = carbon::createfromformat('y-m-d', $jobcard -> dateissued)->format('d/m/y'); $jobcard -> datedespatch = carbon::createfromformat('y-m-d', $jobcard -> datedespatch)->format('d/m/y'); 

i pass $jobcard view. works there several views making use of model , these views use different controllers rather copying code every controller function, want date format automatically converted when pass instance of jobcard view.

the user submit dates in form in d/m/y format need covert y-m-d stored in database.

where best write code this?

you can create acessor called every time access property:

public function getdatereqlatestattribute($value) {     return carbon::createfromformat('y-m-d', $value)->format('d/m/y'); } 

documentation


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -