Eloquent Join Multiple Tables using Laravel 6

Today, We want to share with you Eloquent Join Multiple Tables using Laravel 6.In this post we will show you How to join three table by laravel 6 eloquent model, hear for How to Join Multiple Tables with Eloquent Relationships we will give you demo and example for implement.In this post, we will learn about How to retrieve data from multiple tables with different relations with single query with an example.

Eloquent Join Multiple Tables using Laravel 6

There are the Following The simple About laravel join with multiple conditions Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to get value from two table in laravel, so the Laravel 6.0 Eloquent ORM where join table is used for this example is following below.

Laravel Join Multiple Tables

We have three models:

1) Student.php

<?php

namespace App\Models;
 use Eloquent;

class Student extends Eloquent{

    protected $table = 'students';

    public function teacher()
    {
        return $this->belongsTo('App\Models\Teacher');
    }

    public function team()
    {
        return $this->belongsTo('App\Models\Team');
    }

}

2) Team.php

<?php

namespace App\Models;

use Eloquent;

class Team extends Eloquent
{
    protected $table = "teams";

    public function students()
    {
        return $this->hasMany('App\Models\Student');
    }

}

3) Teacher.php

<?php

namespace App\Models;
use Eloquent;

class Teacher extends Eloquent
{
    protected $table = 'teachers';

    public function students()
    {
        return $this->hasMany('App\Models\Student');
    }

}

Results(Fetch Data):

//retrieve an student by using the teacher and team
$student = \App\Models\Student::with(['teacher','team'])->first();


//retrieve teacher name 
$student->teacher->teacher_name  

//retrieve team name 
$student->team->team_name


$teams = \App\Models\Team::with('students')->get();

$teachers = \App\Models\Team::with('teachers')->get();

Example 2: join three table in Laravel 6

How to join three table by laravel 6 eloquent model

$students = DB::table('students')
    ->select('students.id as articles_id', ..... )
    ->join('teams', 'students.teams_id', '=', 'teams.id')
    ->join('teachers', 'students.user_id', '=', 'teacher.id')
    ->get();

Web Programming Tutorials Example with Demo

Read :

Summary

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

I hope you get an idea about laravel eloquent join 3 tables.
I would like to have feedback on my infinityknow.com 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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Laravel 6 Join multiple tables Eloquent Examples
  2.   How to Convert XML to JSON in PHP?
  3.   Animation (ngAnimate) using Angular Example
  4.   Jquery Angular conflict Solution Example
  5.   Curl GET And POST Method Calls – PHP Example
  6.   Group By with Sum using Laravel Collection
  7.   Laravel 6 get ip address Example
  8.   Laravel 6 left join query Example Tutorial
  9.   How to Join Two Tables in Laravel 6?
  10.   Laravel 6 Eloquent Joins Queries Example Tutorial