adrema/app/Member/Actions/SearchAction.php

34 lines
954 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;
/**
2023-06-14 17:10:50 +02:00
* @return LengthAwarePaginator<int, Member>
2023-02-26 20:51:59 +01:00
*/
2023-08-25 00:23:38 +02:00
public function handle(FilterScope $filter, int $perPage): LengthAwarePaginator
2023-02-26 20:51:59 +01:00
{
2023-08-25 00:23:38 +02:00
return Member::search($filter->search)->query(
fn ($q) => $q->select('*')
->withFilter($filter)
->ordered()
)->paginate($perPage);
2023-02-26 20:51:59 +01:00
}
public function asController(ActionRequest $request): AnonymousResourceCollection
{
2023-08-25 00:23:38 +02:00
return MemberResource::collection($this->handle(FilterScope::fromRequest($request->input('filter', '')), $request->input('per_page', 15)));
2023-02-26 20:51:59 +01:00
}
}