here SQL RIGHT JOIN Syntax
SELECT column_name list
FROM table1
RIGHT JOIN table2 ON table1.column_name=table2.column_name;
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');