Posted inTechnology / Laravel / Mysql / Mysqli / php / Programming

PHP Laravel Eloquent join with subquery

Today, We want to share with you PHP Laravel Eloquent join with subquery.In this post we will show you Laravel Join with Subquery in Query Builder Example, hear for Eloquent Subquery Enhancements in Laravel 6.0 we will give you demo and example for implement.In this post, we will learn about laravel eloquent left join group by with an example.

PHP Laravel Eloquent join with subquery

There are the Following The simple About How to use inner joins with subqueries in Laravel 6 Eloquentffffffff Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6 Joins & Sub Query Joins Example Tutorial, so the How to perform a join containing a sub query on an Eloquent Relationship in laravel 6 is used for this example is following below.

Laravel Fluent Query Builder Join with subquery

using SQL Query

SELECT mobiles.id AS mobile_id
  , mobiles.isbn
  , mobiles.title
  , t_iphone.member_ids
FROM mobiles
  LEFT JOIN (
    SELECT member_mobiles.mobile_id
      , CONCAT('#', GROUP_CONCAT(member_mobiles.member_id SEPARATOR '#,#'), '#') AS member_ids
    FROM member_mobiles
    GROUP BY member_mobiles.mobile_id
  ) AS t_iphone ON t_iphone.mobile_id = mobiles.id
WHERE (t_iphone.member_ids LIKE '%#1#%' OR t_iphone.member_ids LIKE '%#3#%');

using Laravel Eloquent Query: By using the leftJoinSub to construct the query

\App\Models\Mobile class map to the table mobiles

select([
        'member_mobiles.mobile_id',
        \DB::raw('CONCAT(\'#\', GROUP_CONCAT(member_mobiles.member_id SEPARATOR \'#,#\'), \'#\') AS member_ids'),
    ])->groupBy('member_mobiles.mobile_id');

$query = \App\Models\Mobile::select([
    \DB::raw('mobiles.id AS mobile_id'),
    'mobiles.isbn',
    'mobiles.title',
    't_iphone.member_ids',
])
    ->leftJoinSub($subquery, 't_iphone', function ($join) {
        $join->on('t_iphone.mobile_id', '=', 'mobiles.id');
    })
    ->where(function ($q) {
        $q->orWhere('t_iphone.member_ids', 'LIKE', '#' . 1 . '#')
            ->orWhere('t_iphone.member_ids', 'LIKE', '#' . 3 . '#');
    });
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel eloquent left join group by.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype