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

<?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.

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.   Laravel 6 Join Multiple Tables Fetch Data
  2.   Laravel Eloquent Relationships Join Multiple Tables
  3.   How to Open URL in New Tab using CodeIgniter
  4.   laravel join Multiple Tables using Model Example
  5.   Show Multiple Checkbox Checked With Multiple Array Using Php
  6.   Laravel Advance Database Query Builder
  7.   PHP Create directory if it doesn't exist
  8.   Eloquent Right Join Queries in Laravel Example
  9.   how to pick video from gallery in android programmatically?
  10.   Laravel Database Eloquent Unions Query