Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full outer join query in laravel 5.4

Tags:

laravel

I have two table application_requests and transactions in both the tables there may be matching record or may not be. For any condition i want record from both the tables.

In transactions table there is a foreign key column application_request_id (this has value of reference of primary key in application_requests table).. If this condition matched then it should display as one row (or record).

I dont know how to achieve this in laravel.

I have tried below codes but its not working:

    $a = \DB::table('application_requests')->select('id');

    $b  = \DB::table('transactions')->select('application_request_id');

    $results = $b->union($a)->get();

    echo "<pre>";
    print_r($results);die;

transactions table is enter image description here

And my application_requests table is enter image description here

like image 855
VinoCoder Avatar asked Sep 12 '25 05:09

VinoCoder


2 Answers

just like that:

DB::table('transactions')->join('application_requests', 'transactions.application_request_id', '=', 'application_requests.id', 'full outer');
like image 132
Mild Academy Avatar answered Sep 13 '25 21:09

Mild Academy


this is the right code to make a full join:

$second = DB::table('t1')
             ->rightJoin('t2', 't1.t2_id', '=', 't2.id')

$first = DB::table('t1')
            ->leftJoin('t2', 't1.t2_id', '=', 't2.id')
            ->union($first)
            ->get();
like image 28
m.elewa Avatar answered Sep 13 '25 22:09

m.elewa