Laravel GroupBy Issue

Viewed 31

I am looking for a way to return all fields that match together. For example, I have a player_name column, I'm looking to return all rows where the player names match. One player's name will return a collection of their results, and so on and so on for however many there are.

This is what I have tried so far

$test = DB::table('approved_g_r_p_s')
    ->select('player_name', 'cost')
    ->groupBy('player_name')
    ->get();

However it only returned one result from each.

One of player A's fields

One of player B's fields

Despite there being multiple others it has only returned one. Any suggestions?

1 Answers

It looks like you need to add the fields that interest you to the groupBy, something like this:

    $test = DB::table('approved_g_r_p_s')
    ->select('player_name', 'cost')
    ->groupBy('player_name', 'cost')
    ->get();
Related