Calculate age in laravel [duplicate]

2

I want to calculate the age by the date of birth, I will leave the processing in the bank, I am new to Laravel and I am not able to execute the following query:

SELECT YEAR(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dataNascimento))) AS idade FROM pessoas

My code looks something like this:

return view('pessoas.grafico-idade', array('pessoas' => Pessoa::select('pessoa')->select('dataNascimento','YEAR(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dataNascimento))) AS idade')->get()));
    
asked by anonymous 07.04.2018 / 07:33

1 answer

1

try something like:

return view('pessoas.grafico-idade', array('pessoas' => Pessoa::select('pessoa')->select('dataNascimento',DB::raw('YEAR(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dataNascimento))) AS idade'))->get()));

Obs: this will accuse a new error, will say that the DB object was not instantiated. In that case you put this snippet at the beginning of your code:

use Illuminate\Support\Facades\DB;
    
10.04.2018 / 20:16