Fix supplier_user being unique for each supplier and id, fix search looking for users with a supplier_id

This commit is contained in:
Deon George 2022-08-10 17:05:57 +10:00
parent d358620b46
commit f5d535daa7
3 changed files with 9 additions and 22 deletions

View File

@ -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

View File

@ -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',
]);

View File

@ -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