when($this->search, fn ($q) => $q->where('name', 'like', '%'.$this->search.'%')) ->when($this->typeFilter !== 'all' && $this->typeFilter !== 'perfume', fn ($q) => $q->whereNull('id')) ->get() ->map(function ($item) { $item->type_label = 'Parfum'; $item->type = 'perfume'; $item->total_stock = $item->outlets->sum('pivot.stock') + $item->warehouses->sum('pivot.stock'); return $item; }); $products = Product::with(['outlets', 'warehouses']) ->when($this->search, fn ($q) => $q->where('name', 'like', '%'.$this->search.'%')) ->when($this->typeFilter !== 'all' && $this->typeFilter !== 'product', fn ($q) => $q->whereNull('id')) ->get() ->map(function ($item) { $item->type_label = 'Produk'; $item->type = 'product'; $item->total_stock = $item->outlets->sum('pivot.stock') + $item->warehouses->sum('pivot.stock'); return $item; }); $bottles = Bottle::with(['outlets', 'warehouses']) ->when($this->search, fn ($q) => $q->where('name', 'like', '%'.$this->search.'%')) ->when($this->typeFilter !== 'all' && $this->typeFilter !== 'bottle', fn ($q) => $q->whereNull('id')) ->get() ->map(function ($item) { $item->type_label = 'Botol'; $item->type = 'bottle'; $item->total_stock = $item->outlets->sum('pivot.stock') + $item->warehouses->sum('pivot.stock'); return $item; }); $items = $perfumes->concat($products)->concat($bottles)->sortBy('name'); return view('livewire.studio.stock.inventory-history.index', [ 'items' => $items, ]); } }