mandant-crm/backend/app/Modules/Search/Actions/SearchClientsAction.php

36 lines
1 KiB
PHP
Raw Normal View History

<?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') {
return Client::query()
->withCount('historyEntries')
->withMax('historyEntries', 'created_at')
->where(function ($q) use ($query) {
$q->where('name', 'like', "%{$query}%")
->orWhere('client_number', 'like', "%{$query}%");
})
->limit(50)
->get();
}
return Client::query()
->withCount('historyEntries')
->withMax('historyEntries', 'created_at')
->whereRaw(
'MATCH(client_number, name) AGAINST(? IN BOOLEAN MODE)',
[$query.'*']
)
->limit(50)
->get();
}
}