let say I have database table with column of date of birth named dob
dob
then, I want to get all the data using :
$data = MY_Model::all();
How to convert my $data->dob to age (years, month, day) and then store it in $data so I can access it using $data->age ?
$data->dob
$data
$data->age
Create this method on your model:
public function getAgeAttribute() { return Carbon::createFromFormat('Y-m-d', $this->dob)->diffInYears(Carbon::now()); }
Now you can call it from the model data directly as $data->age
1.4m articles
1.4m replys
5 comments
57.0k users