Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda