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

184
Visualizações
Laravel Advanced Wheres how to implement wheres from different table

So I have two table and this is the column that they have

TableA = id, slug, user_id, friend_id, parent_id

TableB = id, user_id, friend_id

This is my example coding

$tableA = TableA::where('slug', '3')->first();

$tableB = TableB::where( function ($query) use ($tableA) {
    $query->where('user_id', $tableA->user_id);
    $query->where('friend_id', $tableA->friend_id);
    $query->where('tableA.parent_id','=','2');
})->exists();

It seems like only this two where conditions are working

$query - > where('user_id', $tableA - > user_id);
$query - > where('friend_id', $tableA - > friend_id);

The last where conditions didn't work. Anyone know what wrong in my coding

$query - > where('tableA.parent_id', '=', '2');

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

try with this query

$query->where($tableA->parent_id, '=', '2');

it's because tableA is a variable, not a relation

about 4 years ago · Santiago Gelvez Relatório

0

In last condition you write condition for tableA. It will not work.

You can try this

$tableA = TableA::where('slug', '3')->where('tableA.parent_id','=','2')->first();

$tableB = TableB::where( function ($query) use ($tableA) {
    $query->where('user_id', $tableA->user_id);
    $query->where('friend_id', $tableA->friend_id);
})->exists();

Also I'm recommending you try make this logic with one query, using join or subquery. You can read this

about 4 years ago · Santiago Gelvez 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