adrema/app/Member/Actions/SearchAction.php

31 lines
853 B
PHP
Raw Normal View History

2023-02-26 20:51:59 +01:00
<?php
namespace App\Member\Actions;
use App\Member\FilterScope;
2023-02-26 20:51:59 +01:00
use App\Member\Member;
use App\Member\MemberResource;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
2023-06-14 17:10:50 +02:00
use Illuminate\Pagination\LengthAwarePaginator;
2023-02-26 20:51:59 +01:00
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SearchAction
{
use AsAction;
/**
2024-01-28 11:42:32 +01:00
* @param array<string, mixed> $filter
2023-06-14 17:10:50 +02:00
* @return LengthAwarePaginator<int, Member>
2023-02-26 20:51:59 +01:00
*/
2024-01-28 11:42:32 +01:00
public function handle(array $filter, int $perPage): LengthAwarePaginator
2023-02-26 20:51:59 +01:00
{
2024-01-28 11:42:32 +01:00
return FilterScope::fromPost($filter)->getQuery()->paginate($perPage);
2023-02-26 20:51:59 +01:00
}
public function asController(ActionRequest $request): AnonymousResourceCollection
{
2024-01-28 11:42:32 +01:00
return MemberResource::collection($this->handle($request->input('filter', []), $request->input('per_page', 15)));
2023-02-26 20:51:59 +01:00
}
}