There is entity User that is stoted in table Users
Some fields in this table are null by default.
I need to update these fields and set not null data.
For this I try to use PATCH method in Laravel:
Routing:
Route::patch('users/update', 'UsersController@update');
Controller:
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
"name" => 'required|string|min:3|max:50',
"email_work" => 'email|max:255|unique:users',
"surname" => 'required|string|min:3|max:50',
"tel" => 'required|numeric|size:11',
"country" => 'required|integer',
"region" => 'required|integer',
"city" => 'required|integer'
]);
if ($validator->fails()) {
return response()->json(["message" => $validator->errors()->all()], 400);
}
$user = User::where("user_id", $id)->update([
"name" => $request->name,
"surname" => $request->surname,
"tel" => $request->tel,
"country" => $request->country,
"city" => $request->city,
"region" => $request->region,
"email_work" => $request->email
]);
return response()->json(["user" => $user]);
}
Does it mean that I can pass any data to update? Should I pass $id parameter to routing and controller relatively?
How to use right handler for PATCH method in Laravel?