admin管理员组文章数量:1300052
Background
I have two models Sale and Shipment with following relation in Sale.
/**
* @return BelongsTo
*/
public function shipment() : BelongsTo
{
return $this->belongsTo( Shipment::class);
}
When sending sale resources via controller .. I have the following SaleResource::toArray method
public function toArray($request)
{
return [
'shipment' => ShipmentResource::make($this->shipment),
/** see comments in question */
// 'shipment' => $this->shipment,
];
}
Shipment object has many attached models , like address, group etc. It has a resource defined with it overridden toArray method
public function toArray($request) {
$shipment = [
'tracking' => $this->tracking
];
if ($request->routeIs('get-picked')) {
$shipment['destination] = AddressResource::make($this->address)
}
return $shipment;
}
The Problem
We are fetching Sale using with
to avoid n+1 issues.
/** @var Builder $query */
$query = Sale::query()->with([
'shipment','shipment.ship_service',
'inventory', 'inventory.book',
'destination' ,
'owner'
]);
However when we use ShipmentResource::make($shipment) , it looks like laravel 8 is provides the resource a shipment object that fetches all related objects again using lazy loading causing n+1 queries.
Notice when we use the commented line, // 'shipment' => $this->shipment,
n+1 issue is completely avoided but laravel does not use ShipmentResource::toArray() method at all .. and instead returns a shipment object without custom fields destination
that are added in ShipmentResource::toArray .
That is a problem because we need the field destination
in front end. Those fields are handled as custom since these are not needed in all requests and we selectively use those based on what route was called.
Any help or pointers in this regard are greatly appreciated. Please let me know if the question requires any further clarification.
本文标签: httpLaravel 8 Resourcemake causing N1 Queries problemStack Overflow
版权声明:本文标题:http - Laravel 8 Resource::make causing N+1 Queries problem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639660a2389841.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论