ItSolutionStuff.com

PHP Laravel Inner Join Query Example

By Hardik Savani • May 14, 2024
Laravel

This post will give you example of laravel inner join query builder. we will help you to give example of inner join query in laravel eloquent. This tutorial will give you simple example of how to use inner join in laravel. this example will help you how to apply inner join in laravel.

So, let's follow few step to create example of how to make inner join in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

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 everywhere in project because of related table.

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

So, let's see bellow example:

Example:

users Table:

countries Table:

SQL Query:

select `users`.`id`, `users`.`name`, `users`.`email`, `countries`.`name` as `country_name`

from `users`

inner join `countries` on `countries`.`id` = `users`.`country_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 index()

{

$users = User::select(

"users.id",

"users.name",

"users.email",

"countries.name as country_name"

)

->join("countries", "countries.id", "=", "users.country_id")

->get();

dd($users);

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Prof. Josiane Jast MD

[email] => savanihd@gmail.com

[country_name] => IN

)

[1] => Array

(

[id] => 2

[name] => Mr. Donavon Beer

[email] => yemmerich@example.net

[country_name] => US

)

[2] => Array

(

[id] => 3

[name] => Judy Watsica

[email] => gutmann.christian@example.org

[country_name] => CA

)

)

Now i will give you best way to use inner join and get country name using Laravel Eloquent Relationship:

Example: Using Relationship

You have to create following models and controller method:

app/User.php

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;

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 Country 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()

{

$users = User::select("*")

->with('country')

->take(3)

->get();

foreach ($users as $key => $value) {

echo $value->id;

echo $value->name;

echo $value->country->name;

}

dd($users);

}

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent whereBetween() Query Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

Laravel Order By Relationship Sum Column Example

Read Now →

Laravel Relationship Eager Loading with Condition Example

Read Now →

Laravel Relationship Where Condition Example

Read Now →

Laravel One to One Eloquent Relationship Tutorial

Read Now →

Laravel One to Many Eloquent Relationship Tutorial

Read Now →

Laravel One to Many Polymorphic Relationship Tutorial

Read Now →

Laravel Where Clause with date_format() Example

Read Now →

Laravel Eloquent Inner Join with Multiple Conditions Example

Read Now →

Laravel Join with Subquery in Query Builder Example

Read Now →