left join in laravel

Posted by w3diy - 6 months ago

PHP Laravel Left Join Query Example

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"
                        )
                        ->leftJoin("countries", "countries.id", "=", "users.country_id")
                        ->get();

        dd($users);
    }
}