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

187
Views
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 answers
Answer question

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 Report

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