How To Use Right Join Clause With Example Using Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

RIGHT JOIN returns all records from the right table and the matched records from the left table . if there is no match returns NULL from left table.RIGHT JOIN SQL statement returns the records which are present in both table, for this there is at least one table column match between two table. So, this type of join we have to make in Laravel framework. There is more benefits of join of multiple table is that in one query execution you can fetch data from multiple table.

How to Use Right Join Clause In Laravel

here SQL RIGHT JOIN Syntax

SELECT column_name list
FROM table1
RIGHT JOIN table2 ON table1.column_name=table2.column_name;
To learn more about SQL, please visit our BajarangiSoft site.

how can we use RIGHT JOIN clause in laravel ?


RIGHT JOIN clause
The query builder may also be used to write join statements. To perform a basic "right join", you may use the join method on a query builder instance. The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. You can even join to multiple tables in a single query:

Example(1)

app/Http/Controllers/LeftJoinTableController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;

class RightJoinTableController extends Controller
{
    function index()
    {
       $student= DB::table('course')
            ->rightJoin('student', 'course.CourseID', '=', 'student.CourseID')
            ->select('course.CourseID', 'student.StudentName','student.Branch')
            ->get();
        return view('student.index', ['student' => $student]);
    }
}
?>

selecting courseid from right table and studentname from left table and branch from table course on RIGHT JOIN of student table by right course table courseid and left student table courseid(course.CourseID = student.CourseID) on order by courseid.


resources/views/student/index.blade.php

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Right Join Multiple Tables</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <br />
    <h3 align="center">Right Join Multiple Tables</h3>
    <br />
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>CourseID</th>
                <th>StudentName</th>
                <th>Branch</th>
            </tr>
            </thead>
            <tbody>
            @foreach($student as $row)
            <tr>
                <td>{{ $row->CourseID }}</td>
                <td>{{ $row->StudentName }}</td>
                <td>{{ $row->Branch }}</td>
            </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

routes/web.php

Route::get('RightTable', 'RightJoinTableController@index');

Related Post