laravel select subquery

Solutions on MaxInterview for laravel select subquery by the best coders in the world

showing results for - "laravel select subquery"
Isabell
12 Aug 2018
11AVG(SUM(amount))
Alessandro
12 Oct 2019
1In addition to @delmadord's answer and your comments:
2
3Currently there is no method to create subquery in FROM clause, so you need to manually use raw statement, then, if necessary, you will merge all the bindings:
4
5$sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance
6
7$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
8    ->mergeBindings($sub->getQuery()) // you need to get underlying Query Builder
9    ->count();
10Mind that you need to merge bindings in correct order. If you have other bound clauses, you must put them after mergeBindings:
11
12$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
13
14    // ->where(..) wrong
15
16    ->mergeBindings($sub->getQuery()) // you need to get underlying Query Builder
17
18    // ->where(..) correct
19
20    ->count();
queries leading to this page
laravel subquery wheresubquerys eloquentlaravel subquery selectlaravel query sub querylaravel subquery fromlaravel eloquent subquery selecthow to subquery in laravelsubquery in select statement laravelsubquery in select statement eloquent laravellaravel select table on subquerylaravel create subquerylaravel subqueriesuse subquery with select in laravellaravel select subquerylaravel select from subsubquery in select method laravelhow to make subquery in select statement laravelsubquery in laraveladd subquery in laravelscript select sub categories laravellaravel eloquent subquerylaravel where subquerysub query in laravel eloquentselect with subquery in from to laravel eloquentsubquery laravel where in elequoent subquery laravelsubquery laravel 8laravel select from subquerysubquery select laravel query buildersubquery in select laravellaravel sub selectlaravel subquery examplesubquery in sql laravelsubquery laravel elquentlaravel from subquery selecthow to write subquery in laravelwith subquery laraveleloquent subquery wherelaravel select query with sub querysubquery laravellaravel eloquent from subquerywrite sub query in laravel selecteloquent subquerylaravel subquery in wherelaravel subquery on query resultlaravel query where in subquery in fieldhow to use sub queries in laravelsubqueries in laravellaravel db subquerylaravel subselecthow to write subquery in laravel selectselect from subquery laravelsubquery select laravellaravel subquery in selectlaravel sub queriessubquery in laravel eloquentsub query in laravelsubquery in eloquent laravellaravel from subqueryeloquent select subquerylaravel 6 subquerylaravel eloquent select subqueryeloquent subquery selectlaravel subqueryquery from sub laravelsubquery in where clause laravelsubqueries laravellaravel subquery usesub query in laravalsub queries laravellaravel wherein subquerylaravel subquery withlaravel with subquerysubselect laravellaravel query subselectlaravel query where in subquerysubquery laravel eloquentsub select query laravelsubquery in where laravelselect subquery in laravel eloquentlaravel where with subquerylaravel eloquent select from subqueryrun subquery in laravellaravel select inside subqueryeloquent laravel subquerysub query laravelwrite sub queries in laravellaravel 8 subquerylaravel select result from subquerylaravel where in subselectlaravel subquery in fromlaravel subquery thissubquery in db select method laravellaravel subquery eloquenthow to write subquery in subquery in laravel 8 eloquentlaravel sub query in slectwhere query laravel subquerywhere subquery laravellaravel sql subquerylaravel sub querysubquey in laraveleloquent subquery laravelselect query conatins subquery in laravellaravel query builder subquerysubquery in laravel in selectselect query laravel with sub tablesub queries in laravelquery builder laravel subquerysub query in laravel 8laravel select subquery query builderlaravel subquery in modellaravel select subquery