join queries in Laravel – onlinecode

join queries in Laravel – onlinecode

In this post we will give you information about join queries in Laravel – onlinecode. Hear we will give you detail about join queries in Laravel – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, We will explain to you how to use the join queries in laravel. normally there are four types of join queries. such as Inner Join, Left Join, Right Join, and Cross Join. but here Advanced Join and Sub-Query Join are possible using the laravel.

Normally we want to need multiple tables data that time we use the join query. so let’s see how to use join query in laravel.

Inner Join query in laravel

The Inner join query method is used to return matching rows data into both tables.

$products = Product::join('categories', 'products.category_id', '=', 'categories.id')
       ->select('products.*')
       ->get();
dd($products);

Left Join query in laravel

The left join query method is used to return all rows data into the left table and return the equal rows of data into the right table.

$products = Product::leftJoin('categories', 'products.category_id', '=', 'categories.id')
       ->select('products.*')
       ->get();
dd($products);

Right Join query in laravel

The right join query method is used to return all rows data into the right table and return the equal rows data into the left table.

$products = Product::rightJoin('categories', 'products.category_id', '=', 'categories.id')
       ->select('products.*')
       ->get();
dd($products);

Cross Join query in laravel

$data = DB::table('sizes')
            ->crossJoin('colours')
            ->get();
dd($data);

Advanced Join Clause

$data = DB::table('users')
        ->join('contacts', function ($join) {
            $join->on('users.id', '=', 'contacts.user_id')
                 ->where('contacts.user_id', '>', 5);
        })
        ->get();
dd($data);

Sub-Query Join Clause

$data = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)->groupBy('user_id');
dd($data);

Please follow and like us:

Hope this code and post will helped you for implement join queries in Laravel – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  4  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US