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

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

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 Denunciar

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 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