Store API - Creating REST API in Laravel(Part IV)

Store Api is an api end point for storing data to database. We are using article as our entity so it will be article store api. In previous article, we have added request validation. We have already created database structure, eloquent model, routes, controller functions. Now, we will add all necessary logic to store function in controller to create store api.

We have already added store 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:

<?php 

namespace App\Http\Controllers;

use App\Article;

use Illuminate\Http\Request;
use App\Http\Requests\ArticleRequest;

class ArticleController extends ApiController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  use Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(ArticleRequest $request)
    {
        try{
            $article  =  new Article();
            $article->title = $request->input('title');
            $article->body = $request->input('body');
            $article->save();

            $result['data'] = $article->toArray();
            $result['status'] = true;

        }catch(\Exception $e){
            $result['status'] = false;
            $result['message'] = 'Operation failed due to '. $e->getMessage();
        }
        
        if($result['status']){
            return $this->success($result['data']);
        }else{
            return $this->fail($result['message']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  use Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ArticleRequest $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

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 store article in the store function.

We have added try catch block to make sure storing of article. For successful storage 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(store 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 POST

2. Set route to http://localhost:8000/api/articles

3. Add json data in body section of request.

Example:

{
   "title":"What is Laravel",
   "body": "Laravel is a php framework based in mvc arcitecture"
}

4. Send the request you will get output like this

{
    "status": "success",
    "status_code": 200,
    "message": "OK",
    "data": {
        "title": "What is Laravel",
        "body": "Laravel is a php framework based in mvc arcitecture",
        "updated_at": "2019-02-09 11:48:03",
        "created_at": "2019-02-09 11:48:03",
        "id": 9
    }
}
Screenshot

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

In the next article, we will discuss about update api for the article.


share :

Sagar Gautam

A Computer Engineer from Nepal.


Leave your Feedback