How To Use Cross Join Clause Statement Using Laravel

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

CROSS JOIN gives a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN.This kind of result is called as Cartesian Product.CROSS 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 Cross Join Clause IN Laravel

here SQL CROSS JOIN Syntax

SELECT *
FROM table1
CROSS JOIN table2;

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


How can we use CROSS JOIN clause in laravel ?

CROSS JOIN  Clause

To perform a "cross join" use the crossJoin method with the name of the table you wish to cross join to. Cross joins generate a cartesian product between the first table and the joined table:

Example(1)

app/Http/Controllers/CrossJoinTableController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;

class CrossJoinTableController extends Controller
{
    function index()
    {
        
        $student = DB::table('course')
            ->crossJoin('student', 'course.CourseID', '=', 'student.CourseID')
            ->select('course.CourseID', 'course.CourseName','student.student_name')
            ->get();
        return view('student.index', ['student' => $student]);
    }
}
?>

selecting courseid and courseName from course table and studentname from student table course on CROSS JOIN of student table with course table  By equaling courseid in both tables and get that which present in both the table in php

resources/views/student/index.blade.php

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Cross 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">Cross 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>CourseName</th>
            </tr>
            </thead>
            <tbody>
            @foreach($student as $row)
            <tr>
                <td>{{ $row->CourseID }}</td>
                <td>{{ $row->Student_name }}</td>
                <td>{{ $row->CourseName }}</td>
            </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

routes/web.php
Route::get('CrossTable', 'CrossJoinTableController@index');

Related Post