2026-06-23 10:10:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Search\Actions;
|
|
|
|
|
|
|
|
|
|
use App\Modules\Client\Models\Client;
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
final class SearchClientsAction
|
|
|
|
|
{
|
|
|
|
|
public function handle(string $query): Collection
|
|
|
|
|
{
|
|
|
|
|
if (DB::connection()->getDriverName() === 'sqlite') {
|
2026-07-18 09:34:18 +00:00
|
|
|
return Client::query()
|
|
|
|
|
->withCount('historyEntries')
|
|
|
|
|
->withMax('historyEntries', 'created_at')
|
|
|
|
|
->where(function ($q) use ($query) {
|
|
|
|
|
$q->where('name', 'like', "%{$query}%")
|
|
|
|
|
->orWhere('client_number', 'like', "%{$query}%");
|
|
|
|
|
})
|
2026-06-23 10:10:28 +00:00
|
|
|
->limit(50)
|
|
|
|
|
->get();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 09:34:18 +00:00
|
|
|
return Client::query()
|
|
|
|
|
->withCount('historyEntries')
|
|
|
|
|
->withMax('historyEntries', 'created_at')
|
|
|
|
|
->whereRaw(
|
|
|
|
|
'MATCH(client_number, name) AGAINST(? IN BOOLEAN MODE)',
|
|
|
|
|
[$query.'*']
|
|
|
|
|
)
|
|
|
|
|
->limit(50)
|
|
|
|
|
->get();
|
2026-06-23 10:10:28 +00:00
|
|
|
}
|
|
|
|
|
}
|