B
B
BonBon Slick2017-01-08 15:59:33
Laravel
BonBon Slick, 2017-01-08 15:59:33

Group identical records and return their count Laravel 5.3?

There are thousands of different records in the table, and it is growing. The structure is something like this:
1592828bfe8746f98820cb8fbf1593cd.png
Now we need to group all columns where the voted_for is the same, returning their number and the string in the voted_for column.
Approximately such a grouping result, 11 is the number of identical Half-Life keys: ['votes' => '11', 'for' => 'Half-Life'].

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BonBon Slick, 2017-01-08
@BonBonSlick

$allVotes = DB::table('votes')
                 ->select('voted_for', DB::raw('count(*) as total'))
                 ->groupBy('voted_for')
                 ->get();
    dd($allVotes);

M
Mysterion, 2017-01-08
@Mysterion

SELECT COUNT(voted_for), voted_for FROM table GROUP BY voted_for;

But I think it could be better.
As a result, you can get two columns - voted_for and the number of duplicates in it.

P
pantagruel964, 2017-01-08
@pantagruel964

$v = App\Model::where('voted_for', 'Half-Life')->get();
$v->count();
See secret link for more information.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question