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?
My Answer:
> You can achieve this by using the `groupBy` method in Laravel. Here's an example of how you can do this:
php
$test = DB::table('approved_g_r_p_s')
->select('player_name', DB::raw('count(*) as total'))
->groupBy('player_name')
->get();
foreach ($test as $player) {
$results = DB::table('approved_g_r_p_s')
->where('player_name', $player->player_name)
->get();
// Do something with the results for each player
}
In this example, we first group the results by the `player_name` field and count the total number of rows for each player. Then, we loop through each player and fetch all the rows that match their name. You can then perform any operations you need with the results for each player.
Rate this post
3 of 5 based on 1028 votesComments