How To Use Inner Join Clause With Example Using Laravel

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

Inner 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 Inner Join Clause In Laravel

here SQL INNER JOIN Syntax

SELECT column_name list
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

To learn more about SQL, please visit our BajarangiSoft site.

how can we use inner join clause in laravel ?

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'));
    }
}
?>

in above example we select employee name and department and his salary by joining different tables and data will be return from controller to index.blade.php file


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');

Related Post