Husniddin's avatar

Eager loading hasOneThrough issue.

I am working with a lot of models connected. But for now telescope showing too many duplicate queries I want to eager load some of them

InventoryItem::query()
            ->select('inventory_items.*')
            ->where('type', '!=', 'inactive')
            ->where('inventory_items.product_id', '=', $product_id)
            ->with([
                'detail.customer',
                'warehouse_location.warehouse',
                'tags',
                'inventory_asset_condition_taxonomy',
                'damaged'
            ])
            ->filter($filters)
            ->orderByDesc('inventory_items.id')
            ->paginate($filters['per_page'] ?? null);
            			
    public function order(): HasOneThrough
    {
        return $this->hasOneThrough(
            Order::class,
            OrderProduct::class,
            'inventory_item_id',
            'id',
            'id',
            'order_id'
        );
    }
    public function orderProducts(): HasMany
    {
        return $this->hasMany(OrderProduct::class, 'order_id', 'id')
            ->whereNull('deleted_at');
    }

I need to join order which connected hasOneThrough and order has orderProducts which connected as HasMany

tried like order.orderProducts and withMorph also but non of them not working

0 likes
0 replies

Please or to participate in this conversation.