Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Verify multivalue with foreach

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
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!