PHP Laravel 6 Inner Join Query Example
Nov 22, 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] => [email protected]
[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] => [email protected]
[email_verified_at] =>
[password] =>
[remember_token] =>
[created_at] =>
[updated_at] =>
[company_name] => google
)
)...........
It will help you....
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial