Laravel 6 Join multiple tables Eloquent Examples

Today, We want to share with you Laravel 6 Join multiple tables Eloquent.In this post we will show you Laravel 6 JOINS Eloquent Tutorial Example From Scratch, hear for ffffffffffffffff we will give you demo and example for implement.In this post, we will learn about Laravel query multiple eloquent relationships with an example.

Laravel’s Eloquent tutorials and tips & tricks

There are the Following The simple About Laravel 6 Eloquent Relationships Tutorial Example From Scratch Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6 JOINS Tutorial INNER OUTER LEFT RIGHT CROSS, so the how to join 3 tables using laravel 6 eloquent is used for this example is following below.

Also Read This πŸ‘‰   Top 40 Laravel Interview Questions

Example 1: Join or Left Join only

Join or Left Join only with where condition in laravel

$studentId = 1050;
$query = Students::SELECT('','','')
->leftJoin('teachers', function($query) use($studentId) {
->where('students.teacher_id', '=', $studentId);

Example 2: Left join add where clause

Laravel Left join add where clause to right table

$students = \DB::table('students')
->select(\DB::raw("students.stid , teachers.local_path as title_image" ))
->leftJoin('teachers',function ($join) {
    $join->on('teachers.owner_stid', '=' , 'students.stid') ;
    $join->where('teachers.relation','=','student_name') ;

Example 3: Inner join with multiple conditions

Laravel 6 – inner join with multiple conditions example using Query Builder

$student = Student::select("students.*"," as itemId"," as teamId")

Example 4: Join with subquery

Laravel 6 Join with subquery in Query Builder Example

$data = DB::table("students")
      GROUP_CONCAT(students_count.year) as year_group,
      GROUP_CONCAT(students_count.age) as age_group
      FROM students_count
      GROUP BY students_count.id_stud
      ) as students_count"),function($join){

Example 5: subquery in select statement

Laravel 6 How to make subquery in select statement?

$data = DB::table("items")
            DB::raw("(SELECT SUM(items_stock.stock) FROM items_stock
                        WHERE items_stock.item_id =
                        GROUP BY items_stock.item_id) as item_stock"),
            DB::raw("(SELECT SUM(items_sell.sell) FROM items_sell
                        WHERE items_sell.item_id =
                        GROUP BY items_sell.item_id) as item_sell"))

Example 6: groupby having with DB::raw

How to use groupby having with DB::raw in Laravel 6 Query Builder?

Also Read This πŸ‘‰   Create Canned Responses using PHP Auto-Replies

$students = DB::table("students")
                ,DB::raw('SUM(students_count.age) as total_age'))

Example 7: Join or Left Join where no foreign key

Join or Left Join where no foreign key – using where condition in Laravel 6

$clientId = 9889;
$query = Students::SELECT('','','')
->leftjoin('teachers', function($query) use($clientId){
$query->on(DB::raw('( '), DB::raw('"'.$clientId.'")'));

Example 8: Left join with last record

Left join with last record of right table in Laravel & Mysql

$query = Students::select('', 'students.user_name','teachers.created_at as last_activity_date')
->leftJoin('teachers', function($query) {
                        ->whereRaw(' IN (select MAX( from teachers as teaca2 join students as us2 on = teaca2.client_id group by');
->where('students.role_type_id', Students::STUDENT_ROLE_TYPE)->get();

Example 9: if else and case when

if else and case when in Laravel 6 query

using If and Else:

Students::SELECT(DB::raw('IF(MIN(students.join_at) IS NULL, students.started_at, MIN(students.join_at)) as join_at'))->get();

using Case When:

Students::SELECT(DB::raw('CASE WHEN COUNT(students.client_id) != 0 THEN COUNT(students.client_id) ELSE 1 END as client_id'))->get();

Example 10: Joins in Laravel Eloquent

Using Joins in Laravel Eloquent Queries

$studentsData = Students::where('id', $studentId)
    ->leftJoin('teachers', 'student.teachers', '=', '')

Web Programming Tutorials Example with Demo

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

Also Read This πŸ‘‰   PHP cURL Set custom header Authorization

I hope you get an idea about Laravel 5.8 Eloquent ORM where join table.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.