Update API – Creating REST API in Laravel(Part V)

Update Api is an api end point for updating records in the database. We are using article as our entity so it will be article update api. In previous article, we have created an store api to store records in database. We have already created database structure, eloquent model, routes, controller functions as well. Now, we will add all necessary logic to update function in controller to create update api.

We have already added update api route in api.php file, you can see this post for reference.

Let’s add the logic in the store function of ArticleController like this:

Now, we have make title and body fillable for the Article model. So add these two columns to fillable array.

At first, we have imported the Article model. Then, we will extend our controller from ApiController so that we can send response based on processing of request. Finally, we have added logic to update article in the update function.

We have added try catch block to make sure update of the article. If requested article is not found then we will return not found with HTTP_NOT_FOUND status. For successful update of the article, we will return success with HTTP_OK status. In case of failure, we will return failed response with error message.

Test using Postman(update api)

I’m using ubantu local server. I’ve hosted my application at localhost:8000. Now, open postman then follow the following steps:

1.Select method type to PATCH

2. Set route to http://localhost:8000/api/articles/8, here 8 is the id of article in my case. It may differ for your case.

3. Add json data in body section of request


4. Send the request you will get output like this


Screenshot Update API

You can find all the details in github: https://github.com/sagautam5/laravel-rest-api

In the next article, we will discuss about api for fetching single article and multiple articles.



Computer Enginner, A Laravel Developer,

Leave a Reply

Your email address will not be published. Required fields are marked *