简体   繁体   中英

Table join and group by in laravel

I am working on a API and I am using resource to return the collection. I am having problem in grouping data. I have following code.

return TicketResource::collection(
                Ticket::query()                    
                    ->search(request('search'))
                    ->whereHas(
                        'latestStatus', function($query) use ($request){
                            $query->whereIn('status_id', $request->status)                            
                            ->where(function($inner_query) use ($request){
                                if($request->status === 1){
                                   $inner_query->where( 'tickets.user_id', '!=', auth()->user()->id);
                                } elseif($request->status === 2){
                                    $inner_query->where( 'ticket_statuses.user_id', auth()->user()->id);
                                }
                            });
                        }
                    )
                    ->join('ticket_statuses', function($join){
                        $join->on('tickets.id', '=', 'ticket_statuses.ticket_id');
                    })
                    ->where(function ($query) use ($userBrands, $userAppliances, $buyerPurchasedTicket, $buyerexcludedZipCode, $buyerexcludedDevices) {
                        $query->whereIn('appliance_id', $userAppliances);
                        $query->whereIn('brand_id',  $userBrands);
                        // $query->whereNotIn('id', $buyerPurchasedTicket);
                        $query->whereNotIn('brand_id', $buyerexcludedDevices);
                        $query->whereNotIn('zip_code_id', $buyerexcludedZipCode);
                    })
                    ->when($request->filters['brand_ids'], fn ($q) => $q->whereIn('brand_id', $request->filters['brand_ids']))
                    ->when($request->filters['appliance_ids'], fn ($q) => $q->whereIn('appliance_id', $request->filters['appliance_ids']))
                    ->when($request->filters['province_ids'], fn ($q) => $q->whereIn('province_id', $request->filters['province_ids']))
                    ->when($request->price, fn ($q) => $q->whereBetween('price', [$request->min_price, $request->max_price]))
                    
                    ->orderBy('tickets.created_at', $request->order ?? 'desc')
                    ->groupBy('ticket_statuses.status_id')
                    ->paginate($request->rows ?? 30)
                )
            ->response()->getData(true);

When I run the above code I get the following error

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'project.tickets.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count( ) as aggregate from (select tickets . from tickets inner join ticket_statuses on tickets . id = ticket_statuses . ticket_id where exists (select * from ticket_statuses inner join (select MAX( ticket_statuses . id ) as id_aggregate , ticket_statuses . ticket_id from ticket_statuses group by ticket_statuses . ticket_id ) as latestOfMany on latestOfMany . id_aggregate = ticket_statuses . id and latestOfMany . ticket_id = ticket_statuses . ticket_id where tickets . id = ticket_statuses . ticket_id and status_id in (1, 2)) and ( appliance_id in (1, 2, 3) and brand_id in (1, 2, 3) and 1 = 1 and 1 = 1) group by ticket_statuses . status_id ) as aggregate_table )"

In your SQL query, your select list must only contains columns that are either appeared in the GROUP BY clause or aggregated columns.

MySQL enables STRICT mode by default, because there would be many weird cases happened without it. See an explanation of the STRICT mode here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM