here SQL INNER JOIN Syntax
SELECT column_name list
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Inner Join Clause
The query builder may also be used to write join statements. To perform a basic "inner 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/JoinTableController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class JoinTableController extends Controller
{
function index()
{
$data = DB::table('salary')
->join('department', 'department.dep_id', '=', 'salary.dep_id')
->join('employee', 'employee.dep_id', '=', 'department.dep_id')
->select('employee.name', 'department.dep_name', 'salary.max_salary')
->get();
return view('index', compact('data'));
}
}
?>
resources/views/index.blade.php
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inner 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">Inner Join Multiple Tables</h3>
<br />
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Employee Name</th>
<th>Department</th>
<th>Maximum Salary</th>
</tr>
</thead>
<tbody>
@foreach($data as $row)
<tr>
<td>{{ $row->name }}</td>
<td>{{ $row->dep_name }}</td>
<td>{{ $row->max_salary }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</body>
</html>
routes/web.php
Route::get('Table', 'JoinTableController@index');