How to Select from SubQuery using distinct in Laravels

  eloquent, laravel

I have a table name "warga" , the table consist:
id (primary key), no_card, name, description. The no_card is a group from some id.
Then, I will count the description based on no_card.
This is the SQL code :

-SQL code :

SELECT description, COUNT(description) as cnt 
FROM ( SELECT distinct no_card, description FROM `warga` ) as t
GROUP by description ;

the SQL already show the data that I need, then in the laravels :

$data =  DB::table('warga')->selectRaw('description , COUNT(description ) AS CountData')
            ->select('no_card', 'description')->distinct()->from('warga')
            ->groupby('description')
            ->get();

When I run the program, the alis table CountData is not detected,
Do you have suggestion to fix it?
Thank you,

Source: Laravel

Leave a Reply