Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda