feat: enhance search functionality in paginated methods to include related product and raw material variants

This commit is contained in:
Yoga Pangestu 2026-08-13 08:48:44 +07:00
parent 5a89a0d1e3
commit 5182dcc012
2 changed files with 6 additions and 2 deletions

View File

@ -41,7 +41,9 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
'productVariants:id,product_id,name,stock,reject_stock,retail_stock',
'productVariants.productPrices:id,variant_id,type,price',
])
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%"))
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%")
->orWhereHas('productVariants', fn ($vq) => $vq->where('name', 'like', "%{$search}%"))
)
->when($filters['name'] ?? null, fn ($q, $name) => $q->where('name', 'like', "%{$name}%"))
->when($filters['status'] ?? null, fn ($q, $status) => $q->where('status', $status))
->when($filters['category'] ?? null, fn ($q, $categoryId) => $q->whereHas('categories', function ($cq) use ($categoryId) {

View File

@ -32,7 +32,9 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
->with([
'rawMaterialPrices:id,raw_material_id,variant,price,stock',
])
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%"))
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%")
->orWhereHas('rawMaterialPrices', fn ($vq) => $vq->where('variant', 'like', "%{$search}%"))
)
->when($filters['name'] ?? null, fn ($q, $name) => $q->where('name', 'like', "%{$name}%"))
->when(($filters['is_active'] ?? null) !== null && ($filters['is_active'] ?? null) !== '', function ($q) use ($filters) {
$q->where('is_active', $filters['is_active'] === 'true');