'Join columns of Day, Month , Year to calculate age
Solution 1:[1]
You can use an "Accessor" to get the age.
- Define a
getAgeAttributemethod that calculate the age in thePatientsmodel - Append the method to the model
class Patients extends Model
{
protected $appends = ['age'];
/**
* Get the patients's age.
*
* @param string $value
* @return string
*/
public function getAgeAttribute($value)
{
$age = (time() - strtotime($this->dobDay.' '.$this->dobMonth.' '. $this->dobYear)) / (60 * 60 * 24 * 365);
$age = floor($age);
return $age;
}
}
And use it like so
public function patients_view($id)
{
$patient = Patients::where('id', '=', $id)->first();
dd($patient->age);
// ....
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

