Laravel 8 Inner Join Query Example

Laravel 8 Inner Join Query Example
Laravel 8 Inner Join Query Example
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
select `users`.`id`, `users`.`name`, `users`.`email`, `countries`.`name` as `country_name` 
from `users`
inner join `countries` on `countries`.`id` = `users`.`country_id`
public function index()
{
$users = User::select('users.id', 'users.name', 'users.email', 'countries.name as country_name')
->join('countries', 'countries.id', '=', 'users.country_id')
->get();
}
use Illuminate\Support\Facades\DB;$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$posts = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)
->groupBy('user_id');
$users = User::join('posts', 'posts.user_id', '=', 'users.id')
->join('comments', 'comments.post_id', '=', 'posts.id')
->get(['users.*', 'posts.descrption']);

--

--

--

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

50 Udemy Paid Courses, Free For Today [6th May]

How to display data using slack custom command together with AWS lambda

DEVITA Global Community-Driven Meme Contest!

What is Kyve?

P²BFT Consensus Algorithm Introduction

How to run your first React Native App on macOS for Android devices

[Leet Code] Maximum Level Sum of a Binary Tree

Hidden mistakes with Shared Element Transitions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.

More from Medium

How to Paginate Query for Laravel API (RestFul)?

Laravel API Pagination

Laravel Basic | How to create Laravel Resource Controller example?

Laravel Passport API that authenticates email or phone number & password

Create Custom Files In Laravel Using Artisan Commands