Adds validator to PATCH request in the API UsersController

This commit is contained in:
Ivan Nieto Vivanco 2023-03-13 15:49:05 -06:00
parent 95f568b934
commit e4204a6dd1

View file

@ -20,6 +20,7 @@ use Auth;
use Illuminate\Http\Request;
use App\Http\Requests\ImageUploadRequest;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
class UsersController extends Controller
{
@ -452,11 +453,12 @@ class UsersController extends Controller
// Check if the request has groups passed and has a value
if ($request->filled('groups')) {
try{
$user->groups()->sync($request->input('groups'));
} catch (\Exception $exception){
return response()->json(Helper::formatStandardApiResponse('error', null, $exception));
}
$validator = Validator::make($request->input('groups'), [
'groups' => 'array',
'groups.*' => 'integer',
]);
$user->groups()->sync($request->input('groups'));
// The groups field has been passed but it is null, so we should blank it out
} elseif ($request->has('groups')) {
$user->groups()->sync([]);