i am trying to make a update API that updates fileds using header auth token
I am new to larvel
this is what I have tried
Route::patch('/update', function (IlluminateHttpRequest $request) {
$data = $request->validate([
'name' => '|max:255',
'phone'=> 'max:255',
'email' => '|email|unique:users',
'period'=> 'max:255',
'babyname'=> 'max:255',
'baby_date'=> 'max:255',
])}) return new IlluminateHttpJsonResponse(['user' => $user] , 200);
})->middleware('auth:api');
3
Answers
Few recommendations:
Use resource route instead of single routes -> https://laravel.com/docs/9.x/controllers#resource-controllers
Read more about validation rules -> https://laravel.com/docs/9.x/validation#available-validation-rules
You can customize the response status:
API Route
UserController
Please let me know if you require further help or clarifications, always happy to help.
Fix your route; it is not found; you have to supply the ID inside the route to reach the controller:
Changes
'name' => '|max:255'
to'name' => 'max:255'
and'email' => '|email|unique:users'
to'email' => 'email|unique:users'
unique()
check added'email|unique:users,email,' . $request->user()->id,
. This will be used to Skip the Current record.return
should be placed inside theRoute()
, not outside.$user = $request->user();
to update the record.Or for the in-detail update.(Without
update()
)