<?php namespace App\Http\Controllers; use Carbon\Carbon; use Illuminate\Http\RedirectResponse; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Session; use App\Http\Requests\{AccountSupplierAdd,UserEdit}; use App\Models\{Account,Supplier,User}; class UserController extends Controller { /** * Update user settings * * @param UserEdit $request * @param User $o * @return RedirectResponse */ public function edit(UserEdit $request,User $o): RedirectResponse { foreach (Arr::except($request->validated(),['password']) as $field => $value) $o->{$field} = $value; if ($x=$request->validated('password')) $o->password = Hash::make($x); return redirect() ->back() ->with('success',($o->isDirty() && $o->save()) ? 'User Updated' : 'No Changes'); } /** * Add a supplier to a user's profile * * @param AccountSupplierAdd $request * @param Account $o * @return RedirectResponse */ public function supplier_addedit(AccountSupplierAdd $request,Account $o): RedirectResponse { $o->suppliers()->attach([ $request->validated('supplier_id') => [ 'supplier_ref'=>$request->validated('supplier_ref'), 'created_at'=>Carbon::now(), ] ]); return redirect() ->back() ->with('success','Supplier Added'); } /** * Remove a supplier from a user's profile * * @param Account $o * @param Supplier $so * @return RedirectResponse */ public function supplier_delete(Account $o,Supplier $so): RedirectResponse { Session::put('supplier_update',TRUE); $o->suppliers()->detach([$so->id]); return redirect() ->back() ->with('success','Supplier Deleted'); } }