PHP Laravel 8 Inner Join Query Example

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
PHP Laravel 8 Inner Join
PHP Laravel 8 Inner Join

Hello Dev’s Today now in this tutorial, I will give you a short example of how to use inner join on query builder in laravel 8 project.We know here how to PHP Laravel 8 Inner Join work. Here i will write a simple inner join query by using laravel 8 query builder. Now we will use db(), join() for write inner join query in laravel 8 application.

So the first argument passed to the join method is the name of the table we need to join to, while the remaining arguments specify the column constraints for the join. we can also even join to multiple tables in a single query.

Now in this example, i will create two table users table and companies table. So i will display to company id to users table.

inner join query builder

public function usersGet()
{
    $users = DB::table('users')
        ->join('companies', 'companies.id', '=', 'users.company_id')
        ->select('users.*', 'companies.company_name')
        ->get()->toArray();    	

        echo '<pre>';
        print_r($users);
        exit;
}

Users Table

you have to create the Users table.

user table

companies Table

you have to create the companies table

companies table

Output

Array(
    [0] => stdClass Object
        (
            [id] => 1
            [company_id] => 1
            [name] => keval
            [email] => kevalkashiyani9@gmail.com
            [email_verified_at] => 
            [password] => 123456
            [remember_token] => 
            [created_at] => 
            [updated_at] => 
            [company_name] => yahoo
        )

    [1] => stdClass Object
        (
            [id] => 2
            [company_id] => 2
            [name] => mehul
            [email] => mehulbagada@gmail.com
            [email_verified_at] => 
            [password] => 
            [remember_token] => 
            [created_at] => 
            [updated_at] => 
            [company_name] => google
        )
)...........

Read Also : Laravel 8 Login with Google Account Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →