From f5d535daa73f72c7087d82aeba6d4b194dce5888 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 10 Aug 2022 17:05:57 +1000 Subject: [PATCH] Fix supplier_user being unique for each supplier and id, fix search looking for users with a supplier_id --- app/Http/Controllers/SearchController.php | 11 +++++++---- app/Http/Controllers/UserController.php | 3 ++- app/Models/Supplier.php | 17 ----------------- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index d0d6773..1eb2c1e 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -44,13 +44,16 @@ class SearchController extends Controller # Look for User by Supplier if (is_numeric($request->input('term'))) - foreach (Supplier::Search($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']) + ->join('suppliers',['suppliers.id'=>'supplier_user.supplier_id']) ->whereIN('user_id',$user_ids) - ->orderBy('name') + ->where('supplier_user.id','like','%'.$request->input('term').'%') + ->orderBy('lastname') + ->orderBy('firstname') ->limit(10)->get() as $o) { - $oo = $o->users->filter(function($item) use ($request) { return str_contains($item->pivot->id,$request->input('term')); })->pop(); - $result->push(['name'=>sprintf('%s (%s:%s)',$oo->name,$o->name,$oo->pivot->id),'value'=>'/u/home/'.$oo->id,'category'=>'Suppliers']); + $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 diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 07a8a2e..fac9e4d 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; +use Illuminate\Validation\Rule; use App\Models\{Supplier,User}; @@ -22,7 +23,7 @@ class UserController extends Controller Session::put('supplier_update',true); $validated = $request->validate([ - 'id'=>'required|string', + 'id'=> ['required','string',Rule::unique('supplier_user')->where(fn ($query) => $query->where('supplier_id',$request->supplier_id)->where('user_id','<>',$o->id))], 'supplier_id'=>'required|exists:suppliers,id', ]); diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index 731ddd5..a2a3a03 100644 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -116,23 +116,6 @@ class Supplier extends Model ->withPivot('id','created_at'); } - /** - * Search for a record - * - * @param $query - * @param string $term - */ - public function scopeSearch($query,string $term) - { - if (is_numeric($term)) { - $query->select('suppliers.*') - ->join('supplier_user',['supplier_user.supplier_id'=>'suppliers.id']) - ->orWhere('supplier_user.id','like','%'.$term.'%'); - } - - return $query; - } - /* METHODS */ public function api_class(): ?string