28 lines
725 B
PHP
28 lines
725 B
PHP
<?php
|
|
|
|
namespace App\Queries;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
final readonly class GetProductsQuery
|
|
{
|
|
public function get(?User $user = null): LengthAwarePaginator
|
|
{
|
|
return Product::query()
|
|
->active()
|
|
->when($user, function (Builder $query) use ($user) {
|
|
$query->withExists(
|
|
[
|
|
'favoritedBy' => fn (Builder $query) => $query->where('user_id', $user->id),
|
|
]
|
|
);
|
|
|
|
})
|
|
->with(['category:id,name,slug', 'images:id,path,product_id'])
|
|
->paginate();
|
|
}
|
|
}
|