0

I have a laravel eloquent db query that I would like to return results based on chained join.

$replies = DB::table('model_replies')
                                ->join('support_tickets', function($join){
                                        $join->on('model_replies.model_id', '=', 'support_tickets.id');
                                        $join->where('model_replies.model', '=', DB::raw('"SupportTicket"'));
                                        })
                                ->join('produce_requests', function($join){
                                        $join->on('model_replies.model_id', '=', 'produce_requests.id');
                                        $join->where('model_replies.model', '=', DB::raw('"ProduceRequest"'));
                                        })

                                ->get();

I followed the example from the docs here.

The problem is when I chain the join statements as suggested here my query gives no results.

A single join works just fine. But I need to be able to chain several of them.

Double join not working

What could I be doing wrong?

Thank you!

4
  • Update, I updated the join statement to this: ->join('produce_requests', function($join){ $join->on('model_replies.model_id', '=', 'produce_requests.id') ->where('model_replies.model', "ProduceRequest"); }) by removing the $join on where as it is in the docs but still nothing changed. Commented Jul 3, 2021 at 11:47
  • shouldn't your $join->where be $join->on? Have a look at this: stackoverflow.com/questions/354070/… Commented Jul 3, 2021 at 11:51
  • @SumitWadhwa $join->on gives same result. Commented Jul 3, 2021 at 11:56
  • did you try ->leftJoin? Commented Jul 3, 2021 at 12:00

1 Answer 1

1

If I understood it correctly. You want to get support_tickets for those model_replies where model_replies.model = SupportTicket

DB::table('model_replies')
     ->leftJoin('support_tickets', function($join){
          $join->on('model_replies.model_id', '=', 'support_tickets.id');
          $join->on('model_replies.model', '=', DB::raw('"SupportTicket"'));
      })
      ->leftJoin('produce_requests', function($join){
          $join->on('model_replies.model_id', '=', 'produce_requests.id');
          $join->where('model_replies.model', '=', DB::raw('"ProduceRequest"'));
       })
       ->get();
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't work because the third argument for $join->on is considered to be a column binding. Even if I do replace that with DB::raw('"SupportTicket"') I still get same result. I can't use two joins.
@KhaKali try with your code but with a ->leftJoin
Thank you sir. I do appreciate your guidance.
@KhaKali you're welcome. the reason ->join didn't work is because it's an inner join, but we needed every row from our main table model_replies hence the leftJoin
@sumit-wadhawa are you able to help me extend this? please check my new question: stackoverflow.com/questions/68238956/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.