vendredi 1 février 2019

How to create different aliases by doing different queries in the same column in Laravel 5.7

I have a table in mysql where I store indexes, and I need to count a column of indices by bringing an alias for each count.

I tried the following code but it does not work, and as I am new to laravel, I'm not sure how I can build this query

$baralho = Baralho::baralho_all();
$data = Baralho::select(
    DB::raw('YEAR(data_entrada) as ano'),
    DB::raw('count(*) as inseridos, id_situacao'),
    DB::raw('count(*) as presos, id_situacao = 2'),
    DB::raw('count(*) as mortos, id_situacao = 3'),
    DB::raw('count(*) as retirados, id_situacao = 4')
)->groupBy('ano')->get();
$geral[] = ['Ano', 'Inseridos', 'Presos', 'Mortos', 'Retirados'];
foreach($data as $key => $value){
    $geral[++$key] = [$value->ano, $value->inseridos, $value->presos, $value->mortos, $value->retirados ];
}
dd($geral);

I need it to count the indexes and return a result for each alias, for example, if in the column id_situacao the index is 2 it counts all 2 indexes and brings me the result with the alias presos so that I can insert those results in the my array $geral[]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire