here SQL CROSS JOIN Syntax
SELECT *
FROM table1
CROSS JOIN table2;
How can we use CROSS JOIN clause in laravel ?
CROSS JOIN Clause
To perform a "cross join" use thecrossJoin
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>
Route::get('CrossTable', 'CrossJoinTableController@index');