I have a column in the table 'games' with the value '1,2,3,4'
I would now like to analyze these IDs in the 'console' table, To check which console it is. Just do not understand why, I only check the first value.
For example if I use the code below, It only displays: "PS4" and not "PS4 XONE PC".
$games = \DB::table('games')
->where('id', '=', $info_games->id_game)
->get();
foreach ($games as $row) {
$cons = \DB::table('console')
->where('id', '=', $row->console)
->get();
}
blade:
@foreach ($cons as $row)
{{ $row->abb_cat }}
@endforeach
When you do this:
$games = \DB::table('games')
->where('id', '=', $info_games->id_game)
->get();
You get one game, with id = $info_games->id_game. So when you do the foreach loop, it's only going to loop one time, for that one game.
Now, that game has a console property with a value of '1,2,3,4'. In this code:
foreach ($games as $row) {
$cons = \DB::table('console')
->where('id', '=', $row->console)
->get();
}
When you refer to $row->console, you're going to get that value 1,2,3,4. That value will be converted to an integer to be used to look up an id in the console table. Because (int) '1,2,3,4' is 1, You only get one record back.
You can get them all by exploding the console value and using that array with whereIn.
$consoles = explode($games->first()->console);
$cons = \DB::table('console')
->whereIn('id', $consoles)
->get();
What would be better is to normalize your database properly and build a many-to-many relationship between games and consoles.
It looks like you are iterating through $games twice: the first time in your php file, and the second in your blade. When you do it in the script, $cons is being overwritten on every iteration. You would like to save them.
$list = [];
foreach ($games as $row) {
$cons = \DB::table('console')
->where('id', '=', $row->console)
->get();
array_push($list, $cons);
}
Then pass $list to your blade.
@foreach ($list as $cons)
{{ $cons->abb_cat }}
@endforeach