Inner Join in laravel eloquent

Now, let’s see blog of how to set inner join in laravel Eloquent. you’ll learn how to make inner join in laravel Eloquent. i explained simply about how to write inner join in laravel Eloquent. you can understand a concept of laravel Eloquent inner join query builder. you will do the following things for inner join query in laravel eloquent.

If you are new in laravel and you don’t know how to write join in laravel application, then i will help you how to make inner join in laravel application. you can easily use inner join in laravel 6 and laravel 7 version.

Inner join is a main part of project. we almost require inner join in laravel Eloquent everywhere in project because of related table.

In this example, i will create users table and student table. i will add student_id on users table and add student tbl id on users table. so when i get users at that time we will get student name from student_id using inner join.

So, let’s see bellow example:

users Table:

stundent Table:

SQL Query:

select `users`.`id`, `users`.`name`, `users`.`email`, `student`.`name` as `student_name` 		                                        
                     from `users` 	
                     inner join `student` on `student`.`id` = `users`.`student_id`

Laravel Query:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function get_data()
    {
        $users = User::select(
                            "users.id", 
                            "users.name",
                            "users.email", 
                            "student.name as student_name"
                        )
                        ->join("student", "student.id", "=", "users.student_id")
                        ->get();
  
        print_r($users);
    }
}

Output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Er. mehjj
            [email] => mehjj@gmail.com
            [student_name] => Er. mehjj
        )

    [1] => Array
        (
            [id] => 2
            [name] => Mr. John Smith
            [email] => join11@example.net
            [student_name] => John
        )

    [2] => Array
        (
            [id] => 3
            [name] => Dipp Yemt
            [email] => yepoi@example.org
            [student_name] => Dipp
        )

)

Example: Using Relationship

app/User.php

<?php
  
namespace App;
  
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
  
class User extends Authenticatable
{
    use Notifiable;
  
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
  
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
  
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
  
    /**
     * Get the comments for the blog post.
     */
    public function country()
    {
        return $this->belongsTo(Country::class);
    }
}

app/Country.php

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Student extends Model
{
        
}

Laravel Query:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
   
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = User::select("*")
                        ->with('student')
                        ->take(3)
                        ->get();
  
        foreach ($data as $key => $value) {
            echo $value->id;
            echo $value->name;
            echo $value->student->name;
        }

        dd($users);
    }
}

I hope it can help you… please reply back

1 thought on “Inner Join in laravel eloquent”

Leave a Comment