How To Use Left Join Clause With Example Using Laravel

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

The LEFT JOIN keyword returns all records from the left table, and the matched records from the right table. The result is NULL from the right side, if there is no match. LEFT 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 Left Join Clause In Laravel

here SQL Left JOIN Syntax

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

how can we use left join clause in laravel ?

Left Join  Clause

The query builder may also be used to write join statements. To perform a basic "left 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 LeftJoinTableController extends Controller
{
    function index()
    {
        $student = DB::table('course')
            ->leftJoin('student', 'course.CourseID', '=', 'student.CourseID')
            ->select('course.CourseName', 'student.StudentID')
            ->get();
        return view('user.index', ['student' => $student]);
    }
}
?>

in this above example from "course" table selecting CourseName and  from "student" table selecting StudentID from left course table  joing right student table if there is courseid in student table which is in course table than it will fetch data to index.blade.php file or else it will return NULL. 

resources/views/student/index.blade.php

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Left 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">Left Join Multiple Tables</h3>
    <br />
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <thead>
            <tr>
                <th>StudentID</th>
                <th>CourseName</th>
            </tr>
            </thead>
            <tbody>
            @foreach($student as $row)
            <tr>
                <td>{{ $row->StudentID }}</td>
                <td>{{ $row->CourseName }}</td>
            </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>
</body>
</html>
 

routes/web.php

Route::get('LeftTable', 'LeftJoinTableController@index');

Related Post