adrema/app/Member/Actions/SearchAction.php

41 lines
1.0 KiB
PHP
Raw Normal View History

2023-02-26 20:51:59 +01:00
<?php
namespace App\Member\Actions;
use App\Member\Member;
use App\Member\MemberResource;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SearchAction
{
use AsAction;
/**
2023-02-26 21:47:52 +01:00
* @return Collection<int, Member>
2023-02-26 20:51:59 +01:00
*/
public function handle(string $search): Collection
{
return Member::search($search)->query(fn ($query) => $query->ordered())->get();
}
public function asController(ActionRequest $request): AnonymousResourceCollection
{
if ($request->input('minLength') !== null && strlen($request->input('search')) < $request->input('minLength')) {
return MemberResource::collection($this->empty());
}
return MemberResource::collection($this->handle($request->input('search', '')));
}
/**
2023-02-26 21:47:52 +01:00
* @return Collection<int, Member>
2023-02-26 20:51:59 +01:00
*/
private function empty(): Collection
{
return Member::where('id', -1)->get();
}
}