From 70e94bf6e61d785b184c8d755a58b5ccd4f2888c Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 6 Jul 2024 22:56:51 +1000 Subject: [PATCH] Fix search and psql like queries need to be ilike for case insensitivity --- app/Console/Commands/BroadbandTraffic.php | 2 +- app/Http/Controllers/SearchController.php | 22 +++++++++++----------- app/Models/Account.php | 8 ++++---- app/Models/Invoice.php | 2 +- app/Models/Service.php | 16 ++++++++-------- app/Models/User.php | 14 +++++++------- app/Traits/ServiceDomains.php | 2 +- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/Console/Commands/BroadbandTraffic.php b/app/Console/Commands/BroadbandTraffic.php index b162050..89cb6be 100644 --- a/app/Console/Commands/BroadbandTraffic.php +++ b/app/Console/Commands/BroadbandTraffic.php @@ -32,7 +32,7 @@ class BroadbandTraffic extends Command public function handle() { if ($this->option('supplier')) { - $o = Supplier::where('name','like',$this->option('supplier'))->singleOrFail(); + $o = Supplier::where('name','ilike',$this->option('supplier'))->singleOrFail(); Job::dispatchSync($o->id); return; diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 8246a5a..cd9c1c2 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -7,7 +7,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Gate; -use App\Models\{Account,Invoice,Payment,Service,Supplier,User}; +use App\Models\{Account,Invoice,Payment,Service,User}; class SearchController extends Controller { @@ -29,10 +29,10 @@ class SearchController extends Controller if (! $request->input('term')) return $result; - $account_ids = ($x=Auth::user()->accounts)->pluck('id'); - $user_ids = $x->transform(function($item) { return $item->user;})->pluck('id'); + $account_ids = ($x=Auth::user()->accounts_all)->pluck('id'); + $user_ids = $x->pluck('user_id')->unique(); - # Look for User + // Look for User foreach (User::Search($request->input('term')) ->whereIN('id',$user_ids) ->orderBy('lastname') @@ -42,7 +42,7 @@ class SearchController extends Controller $result->push(['name'=>sprintf('%s (%s) - %s',$o->name,$o->lid,$o->email),'value'=>'/u/home/'.$o->id,'category'=>'Users']); } - # Look for User by Supplier + // Look for User by their Supplier ID with some suppliers if (is_numeric($request->input('term'))) foreach (User::select(['users.*','suppliers.name AS supplier_name','supplier_user.id AS pivot_id']) ->join('supplier_user',['supplier_user.user_id'=>'users.id']) @@ -56,16 +56,16 @@ class SearchController extends Controller $result->push(['name'=>sprintf('%s (%s:%s)',$o->name,$o->supplier_name,$o->pivot_id),'value'=>'/u/home/'.$o->id,'category'=>'Suppliers']); } - # Look for Account + // Look for Account foreach (Account::Search($request->input('term')) - ->whereIN('user_id',$user_ids) + ->whereIN('id',$account_ids) ->orderBy('company') ->limit(10)->get() as $o) { - $result->push(['name'=>sprintf('%s (%s)',$o->company,$o->lid),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']); + $result->push(['name'=>sprintf('%s (%s)',$o->name,$o->lid),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']); } - # Look for a Service + // Look for a Service foreach (Service::Search($request->input('term')) ->whereIN('account_id',$account_ids) ->orderBy('id') @@ -74,7 +74,7 @@ class SearchController extends Controller $result->push(['name'=>sprintf('%s (%s) %s',$o->name,$o->lid,$o->active ? '' : 'INACT'),'value'=>'/u/service/'.$o->id,'category'=>$o->category_name]); } - # Look for an Invoice + // Look for an Invoice foreach (Invoice::Search($request->input('term')) ->whereIN('account_id',$account_ids) ->orderBy('id') @@ -84,7 +84,7 @@ class SearchController extends Controller } if (Gate::any(['wholesaler'],new Payment)) { - # Look for Payments + // Look for Payments foreach (Payment::Search($request->input('term')) ->whereIN('account_id',$account_ids) ->limit(10)->get() as $o) diff --git a/app/Models/Account.php b/app/Models/Account.php index 461d3fa..9fc2c1e 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -180,10 +180,10 @@ class Account extends Model implements IDs $query->where('id','like','%'.$term.'%'); } else { - $query->where('company','like','%'.$term.'%') - ->orWhere('address1','like','%'.$term.'%') - ->orWhere('address2','like','%'.$term.'%') - ->orWhere('city','like','%'.$term.'%'); + $query->where('company','ilike','%'.$term.'%') + ->orWhere('address1','ilike','%'.$term.'%') + ->orWhere('address2','ilike','%'.$term.'%') + ->orWhere('city','ilike','%'.$term.'%'); } return $query; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 75677ee..1957351 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -38,7 +38,7 @@ class Invoice extends Model implements IDs protected $casts = [ 'created_at' => 'datetime:Y-m-d', 'due_at' => 'datetime:Y-m-d', - 'reminders'=>'json', + 'reminders' => 'json', '_paid_at' => 'datetime:Y-m-d', ]; diff --git a/app/Models/Service.php b/app/Models/Service.php index 9e88073..4200f5c 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -530,18 +530,18 @@ class Service extends Model implements IDs return $query->select('services.*') ->where('services.id','like',$t) ->leftJoin('service_broadband',['service_broadband.service_id'=>'services.id']) - ->orWhere('service_broadband.service_number','like',$t) - ->orWhere('service_broadband.service_address','like',$t) - ->orWhere('service_broadband.ipaddress','like',$t) + ->orWhere('service_broadband.service_number','ilike',$t) + ->orWhere('service_broadband.service_address','ilike',$t) + ->orWhere('service_broadband.ipaddress','ilike',$t) ->leftJoin('service_phone',['service_phone.service_id'=>'services.id']) - ->orWhere('service_phone.service_number','like',$t) - ->orWhere('service_phone.service_address','like',$t) + ->orWhere('service_phone.service_number','ilike',$t) + ->orWhere('service_phone.service_address','ilike',$t) ->leftJoin('service_domain',['service_domain.service_id'=>'services.id']) - ->orWhere('service_domain.domain_name','like',$t) + ->orWhere('service_domain.domain_name','ilike',$t) ->leftJoin('service_email',['service_email.service_id'=>'services.id']) - ->orWhere('service_email.domain_name','like',$t) + ->orWhere('service_email.domain_name','ilike',$t) ->leftJoin('service_host',['service_host.service_id'=>'services.id']) - ->orWhere('service_host.domain_name','like',$t); + ->orWhere('service_host.domain_name','ilike',$t); } /* ATTRIBUTES */ diff --git a/app/Models/User.php b/app/Models/User.php index adf6e1f..aca9d13 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -189,27 +189,27 @@ class User extends Authenticatable implements IDs { // Build our where clause // First Name, Last name - if (preg_match('/\ /',$term)) { + if (preg_match('/\s+/',$term)) { [$fn,$ln] = explode(' ',$term,2); $query->where(function($query1) use ($fn,$ln,$term) { $query1->where(function($query2) use ($fn,$ln) { return $query2 - ->where('firstname','like','%'.$fn.'%') - ->where('lastname','like','%'.$ln.'%'); + ->where('firstname','ilike','%'.$fn.'%') + ->where('lastname','ilike','%'.$ln.'%'); }); }); } elseif (is_numeric($term)) { $query->where('users.id','like','%'.$term.'%'); - } elseif (preg_match('/\@/',$term)) { - $query->where('email','like','%'.$term.'%'); + } elseif (preg_match('/@/',$term)) { + $query->where('email','ilike','%'.$term.'%'); } else { $query - ->Where('firstname','like','%'.$term.'%') - ->orWhere('lastname','like','%'.$term.'%'); + ->Where('firstname','ilike','%'.$term.'%') + ->orWhere('lastname','ilike','%'.$term.'%'); } return $query; diff --git a/app/Traits/ServiceDomains.php b/app/Traits/ServiceDomains.php index 6e48122..a1f368f 100644 --- a/app/Traits/ServiceDomains.php +++ b/app/Traits/ServiceDomains.php @@ -54,6 +54,6 @@ trait ServiceDomains // Build our where clause return parent::scopeSearch($query,$term) - ->orwhere('domain_name','like','%'.$term.'%'); + ->orwhere('domain_name','ilike','%'.$term.'%'); } } \ No newline at end of file