PHP Laravel 6 Inner Join Query Example

Laravel 6

Nicesnippets

13950

22-11-2019


hi guys,

In this tutorial, I will learn you to use inner join query builder in laravel 6 project. i will write simple inner join query using laravel 6 query builder. we will use db(), join() to write inner join query in laravel 6.

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

In this example, i will create two tables users and companies. 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.

companies Table

you have to create the 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

)

)...........

It will help you....


Recommended Posts