PHP Laravel Join Query Example

Today, We want to share with you join query in laravel.In this post we will show you laravel left join multiple conditions, hear for used belongsTo(), you can use hasOne, hasMany as you need we will give you demo and example for implement.In this post, we will learn about Laravel 6 right join query Example Tutorial with an example.

PHP Laravel Left Join Query Example

SQL Query:

select `members`.`id`, `members`.`name`, `members`.`email`, `locations`.`name` as `location_name` 
	from `members` 
	left join `locations` on `locations`.`id` = `members`.`location_id`

Laravel Query:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Member;
  
class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $members = Member::select(
                            "members.id", 
                            "members.name",
                            "members.email", 
                            "locations.name as location_name"
                        )
                        ->leftJoin("locations", "locations.id", "=", "members.location_id")
                        ->get();
  
        dd($members);
    }
}

Results

Array

(

    [0] => Array

        (

            [id] => 1
            [name] => Prof. Chirag Prajapati JD
            [email] => [email protected]
            [location_name] => IN

        )

    [1] => Array

        (

            [id] => 2
            [name] => Mr. Meet Parmara
            [email] => [email protected]
            [location_name] => US

        )

    [2] => Array

        (

            [id] => 3
            [name] => Raghav Domadiya
            [email] => [email protected]
            [location_name] => CA

        )

    [3] => Array

        (

            [id] => 4
            [name] => Miss Parag Shukla J
            [email] => [email protected]
            [location_name] => 

        )

)

Example: Using Relationship

app/Member.php

<?php
  
namespace App;
  
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\Member as Authenticatable;
use Illuminate\Notifications\Notifiable;
  
class Member 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 location()
    {
        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\Member;
   
class MemberController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $members = Member::select("*")
                        ->with('location')
                        ->get();
  
        foreach ($members as $key => $value) {
            echo $value->id;
            echo $value->name;
            echo $value->location->name ?? '-';
        }
        dd($members);
    }
}

I hope you get an idea about laravel subquery.
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.

Read Also:  Eloquent Right Join Queries in Laravel Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP Laravel 7 Inner Join Query Example (join query in laravel)
  2. Read Also:  How to get last record of database table in Laravel?
  3. Read Also:  Laravel Advance Database Query Builder
  4. Read Also:  Laravel 6 Eloquent Query Retrieving Single Models
  5. Read Also:  Laravel 6 tree view Plugins using jQuery Example
  6. Read Also:  jQuery Ajax GET & POST REQUEST Methods PHP MySQLi
  7. Read Also:  call to undefined function base_url()
  8. Read Also:  PHP MySQL Autocomplete textbox using jQuery
  9. Read Also:  PHP Laravel Paths Example Tutorial From Scratch
  10. Read Also:  PHP Laravel Multiple Where Condition using Eloquent
CLOSEX