osb/app/Http/Controllers/UserController.php

74 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2022-08-07 02:17:20 +00:00
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
2022-08-07 02:17:20 +00:00
use Illuminate\Support\Facades\Session;
use App\Http\Requests\{AccountSupplierAdd,UserEdit};
use App\Models\{Account,Supplier,User};
2022-08-07 02:17:20 +00:00
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');
}
2022-08-07 02:17:20 +00:00
/**
* Add a supplier to a user's profile
*
* @param AccountSupplierAdd $request
* @param Account $o
* @return RedirectResponse
2022-08-07 02:17:20 +00:00
*/
public function supplier_addedit(AccountSupplierAdd $request,Account $o): RedirectResponse
2022-08-07 02:17:20 +00:00
{
$o->suppliers()->attach([
$request->validated('supplier_id') => [
'supplier_ref'=>$request->validated('supplier_ref'),
2022-08-07 02:17:20 +00:00
'created_at'=>Carbon::now(),
]
]);
return redirect()
->back()
->with('success','Supplier Added');
2022-08-07 02:17:20 +00:00
}
/**
* Remove a supplier from a user's profile
*
* @param Account $o
2022-08-07 02:17:20 +00:00
* @param Supplier $so
* @return RedirectResponse
2022-08-07 02:17:20 +00:00
*/
public function supplier_delete(Account $o,Supplier $so): RedirectResponse
2022-08-07 02:17:20 +00:00
{
Session::put('supplier_update',TRUE);
2022-08-07 02:17:20 +00:00
$o->suppliers()->detach([$so->id]);
return redirect()
->back()
->with('success','Supplier Deleted');
2022-08-07 02:17:20 +00:00
}
}