Calculate age in Standard

Asked

Viewed 705 times

2

I want to calculate the age through the date of birth, I will leave the processing in the database, I am new in 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 is more or less 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()));
  • @Virgilionovic I’ve looked at this quest before I open this question, I’m trying to do in Laravel. But I’ve already solved it here anyway.

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()));

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

use Illuminate\Support\Facades\DB;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.