2022-08-07 02:17:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2022-08-10 07:05:57 +00:00
|
|
|
use Illuminate\Validation\Rule;
|
2022-08-07 02:17:20 +00:00
|
|
|
|
|
|
|
use App\Models\{Supplier,User};
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Add a supplier to a user's profile
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param User $o
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function supplier_addedit(Request $request,User $o)
|
|
|
|
{
|
|
|
|
Session::put('supplier_update',true);
|
|
|
|
|
|
|
|
$validated = $request->validate([
|
2022-08-10 07:05:57 +00:00
|
|
|
'id'=> ['required','string',Rule::unique('supplier_user')->where(fn ($query) => $query->where('supplier_id',$request->supplier_id)->where('user_id','<>',$o->id))],
|
2022-08-07 02:17:20 +00:00
|
|
|
'supplier_id'=>'required|exists:suppliers,id',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$o->suppliers()->attach([
|
|
|
|
$validated['supplier_id'] => [
|
|
|
|
'id'=>$validated['id'],
|
|
|
|
'site_id'=>$o->site_id,
|
|
|
|
'created_at'=>Carbon::now(),
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success','Supplier Added');
|
2022-08-07 02:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a supplier from a user's profile
|
|
|
|
*
|
|
|
|
* @param User $o
|
|
|
|
* @param Supplier $so
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function supplier_delete(User $o,Supplier $so)
|
|
|
|
{
|
|
|
|
Session::put('supplier_update',true);
|
|
|
|
|
|
|
|
$o->suppliers()->detach([$so->id]);
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success','Supplier Deleted');
|
2022-08-07 02:17:20 +00:00
|
|
|
}
|
|
|
|
}
|