Kha Kali
Kha Kali

Reputation: 189

Laravel eloquent double join returning empty

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!

Upvotes: 0

Views: 181

Answers (1)

Sumit Wadhwa
Sumit Wadhwa

Reputation: 3217

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();

Upvotes: 1

Related Questions