Laravel 6 SubQuery MySQL Tutorial with Examples

Today, We want to share with you Laravel 6 SubQuery MySQL Tutorial with Examples.In this post we will show you Laravel 6 Joins & Sub Query Joins Example Tutorial, hear for laravel subquery in where clause we will give you demo and example for implement.In this post, we will learn about Complete guide of using Laravel 6 Eloquent Subquery Enhancements with an example.

Laravel 6 SubQuery MySQL Tutorial with Examples

There are the Following The simple About laravel subquery relationship Full Information With Example and source code.

Read Also:  PHP Laravel Eloquent join with subquery

As I will cover this Post with live Working example to develop Eloquent Subquery Enhancements in Laravel 6.2, so the Laravel 6 search query with multiple conditions is used for this example is following below.

Sub-Query Joins in Laravel 6

DB::table('members')
->select('member_id', DB::raw('MAX(join_date) as last_member_join_date'))
->where('is_status', true)->groupBy('member_id');

Get Member Last Task Date:

App\Member::addSelect(['last_task' => Tasks::select('join_date')
    ->whereColumn('member_id', 'members.id')
    ->orderBy('join_date', 'desc')
    ->limit(1)])
    ->find(1);

HOW TO SELECT FROM SUBQUERY USING LARAVEL QUERY BUILDER?

App\Member::addSelect(['last_task' => Tasks::select('join_date')
    ->whereColumn('member_id', 'members.id')
    ->orderBy('join_date', 'desc')
    ->limit(1)])
    ->get();

use closure function for eloquent subqueries

App\Member::addSelect(['last_task' => function ($query) {
    $query->select('join_date')
        ->fromt('tasks')
        ->whereColumn('member_id', 'members.id')
        ->orderBy('join_date', 'desc')
        ->limit(1);
}])->get();

Get Member Resent Team

App\Member::addSelect(['last_join_id' => Team::select('id')
    ->whereColumn('member_id', 'members.id')
    ->orderBy('join_date', 'desc')
    ->limit(1)])
    ->find(1);

App\Member::addSelect(['last_join_id' => function ($query) {
    $query->select('join_date')
        ->fromt('teams')
        ->whereColumn('member_id', 'members.id')
        ->orderBy('join_date', 'desc')
        ->limit(1);
}])->find(1);

Apply Eloquent Subquery to OrderBy clause:

App\Member::orderByDesc(Tasks::select('join_date')
    ->whereColumn('member_id', 'members.id')
    ->latest()
    ->limit(1))
    ->get();

change orderByDesc() to orderBy().

App\Member::orderBy(function ($query) {
    $query->select('join_date')
        ->whereColumn('member_id', 'members.id')
        ->latest()
        ->limit(1);
})->get();

App\Member::addSelect(['last_join_id' => function ($query) {
    $query->select('join_date')
        ->fromt('teams')
        ->whereColumn('member_id', 'members.id')
        ->orderBy('join_date', 'desc')
        ->limit(1);
}])->find(1);

Laravel – How to make subquery in select statement?

Laravel 6 Query Builder:

$teams_data = DB::table("teams")
->select("teams.*",
    DB::raw("(SELECT SUM(teams_stock.stock) FROM teams_stock
                WHERE teams_stock.team_id = teams.id
                GROUP BY teams_stock.team_id) as team_stock"),
    DB::raw("(SELECT SUM(teams_sell.sell) FROM teams_sell
                WHERE teams_sell.team_id = teams.id
                GROUP BY teams_sell.team_id) as team_sell"))
->get();
dd($teams_data);

Web Programming Tutorials Example with Demo

Read :

Read Also:  How to Configure Virtual Host in XAMPP

Summary

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

I hope you get an idea about Laravel 6 search query with multiple conditions.
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:  DataTables Pagination Server-side Processing using PHP and MySQL
  2. Read Also:  Angular 2 interview questions Answers
  3. Read Also:  PHP Laravel Join Query Example
  4. Read Also:  Laravel 6 Joins Example Tutorial
  5. Read Also:  Laravel AJAX Tutorial and MySQL Example
  6. Read Also:  Eloquent Left Join Queries in Laravel Examples
  7. Read Also:  Get Current login Auth user details using Laravel 5.7
  8. Read Also:  Convert Json String to Array in VueJS
  9. Read Also:  Laravel 6 Join Multiple Tables Fetch Data
  10. Read Also:  Laravel 6 Eloquent WHERE Like query Example
CLOSEX