Home > Net >  How To Count a Column That Has Three Different Values Inside In Laravel
How To Count a Column That Has Three Different Values Inside In Laravel

Time:07-25

hi guys i have a problem i want to count a column that has 3 values the column is result it has accepted refused and pending and I want to count each one of them on three cards is there are anyways to do so I'm using laravel 8 btw thank you for any tips

CodePudding user response:

Generally you may use some kind of conditional aggregation. Here is a raw MySQL query which should work:

SELECT grp,
       SUM(col = 'accepted') AS num_accepted,
       SUM(col = 'refused')  AS num_refused,
       SUM(col = 'pending')  AS num_pending
FROM yourTable
GROUP BY grp;

CodePudding user response:

YourModel::groupBy('col')
->selectRaw("SUM(`col` LIKE 'value1') as value1, SUM(`col` LIKE 'value2') as value2, SUM(`col` LIKE 'value3') as value3")
->get();

in SUM you can write any condition.

  • Related