Skip to content

nnaemekanweke/Laravel-API

Repository files navigation

How to build a Laravel API

This is a simple laravel API which includes API versioning (v1 and v2).

Getting started

  1. First, clone this repo
  2. CD into the directory
  3. Run composer install
  4. Run php artisan key:generate
  5. Run table migrations php artisan migrate
  6. Seed the database with some random data php artisan db:seed
  7. Run php artisan serve

How to use (v1)

  • Fetch index of all data in Person table with pagination
    localhost:8000/api/v1/people
    Method GET

  • Fetch single data
    localhost:8000/api/v1/person/5
    Method GET
    Sample response

    {
    "data": {
        "id": 5,
        "first_name": "Lenna",
        "last_name": "Jacobs",
        "phone": "1-578-480-5526 x210",
        "email": "sporer.prudence@example.net",
        "country": "Cocos (Keeling) Islands",
        "created_at": "2020-11-07T20:10:56.000000Z",
        "updated_at": "2020-11-07T20:10:56.000000Z"
        }
    }
  • Store new data
    localhost:8000/api/v1/person
    Method POST
    You can pass this in form-data of postman.

    {
       "first_name":
       "last_name":
       "email" :
       "phone" :
       "country" :
    }
    
  • Update a single record
    localhost:8000/api/v1/person/1
    Method PUT
    Sample Body
    {
        "first_name" : "Nnaemeka",
        "last_name" : "Nweke",
        "phone" : "1234567890",
        "email" : "nnaemeka@example.com",
        "country" : "Nigeria"
    }
    
  • Delete a single record
    localhost:8000/api/v1/person/1
    Method DELETE

How to use (v2)

  • Fetch single data
    localhost:8000/api/v2/person/1
    Method GET
    Sample response
   {
    "data": {
        "full_name": "Nnaemeka Nweke",
        "phone": "1234567890",
        "email": "nnaemeka@example.com",
        "country": "Nigeria"
       }
   }