adrema/app/Member/Actions/SearchAction.php

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