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;
Left Join Clause
The query builder may also be used to write join statements. To perform a basic "left join", you may use thejoin
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: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');