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.

Read Also:  Laravel 6 Join Clause Example Tutorial

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

Read Also:  Laravel 6.2 Eloquent Relationship Tutorial

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

<?php

// inner select
$subquery = \DB::table('member_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.

Read Also:  Bootstrap 4 Carousel Slider With Thumbnails

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Get Responsive navbar menu to Automatically close after anchor link clicked
  2. Read Also:  Laravel Increment and Decrement Example
  3. Read Also:  Eloquent Left Join Queries in Laravel Examples
  4. Read Also:  Laravel 6 Multiple Where Eloquent Query
  5. Read Also:  Go Program to Print Pyramid Pattern using numbers
  6. Read Also:  Laravel 7 jQuery Ajax Request Tutorial
  7. Read Also:  Laravel 6 SubQuery MySQL Tutorial with Examples
  8. Read Also:  sql multiple subqueries in (select statement)
  9. Read Also:  Increment Decrement with Laravel 6 Example
  10. Read Also:  PHP Laravel Join Query Example
CLOSEX