Pre-requisites
We'll assume 3 things:
- That the model was created using the approach in this blog post.
- That the route was created using the approach in this blog post.
- That a migration file has been set using this blog post.
- That an Eloquent API Resource has been set up using this blog post.
Similar to the approach to add data we will update the
Http/Requests/UpdateYourRequest.php
.We can begin by removing everything from within the class.
The we will instead extend the class by StoreYourRequest
Now open the
YourController.php
and remove the edit method.In the
function update()
thus:public function update(UpdateYourRequest $request, YourModel $yourmodel)
{
$yourmodel->update($request->validated());
return YourResource::make($yourmodel);
}
To test this in Postman, you'll need to send a PUT request with the URL of
http://localhost/api/yourcontroller/1
In the Headers tab add the key Accept and the value of
application/json
In the body select raw and choose the data format JSON.
Try using an empty object to see if the API returns the validation message
Now try an object like this:
{
"name": "My name again"
}
That should work.
No comments:
Post a Comment