DEV Community

pcreem
pcreem

Posted on • Edited on

Build a simple Laravel API

A Controller with an API

$laravel new simpleAPI
$cd simpleAPI
$php artisan make:controller dummyAPI
$code app/Http/Controllers/dummyAPI.php
Enter fullscreen mode Exit fullscreen mode

app/Http/Controllers/dummyAPI.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class dummyAPI extends Controller
{
    function getData(){
        return ["name"=>"alice"];
    }
}

Enter fullscreen mode Exit fullscreen mode

routes/api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\dummyAPI;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::get("data",[dummyAPI::class,'getData']);

Enter fullscreen mode Exit fullscreen mode

$php artisan serve
Get http://127.0.0.1:8000/api/data on Postman or Browser
the output

{
    "name": "alice"
}
Enter fullscreen mode Exit fullscreen mode

Controller plus Model and API
create database and table, use mysql as example

mysql> create database dummy;
$php artisan make:model Dummy -rmc
Model created successfully.
Created Migration: 2021_02_09_094710_create_dummies_table
Controller created successfully.
Enter fullscreen mode Exit fullscreen mode

modify database/migrations/2021_02_09_094710_create_dummies_table.php

  public function up()
    {
        Schema::create('dummies', function (Blueprint $table) 
        {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }
Enter fullscreen mode Exit fullscreen mode

migrate and insert data

$php artisan migrate
mysql> use dummy;
mysql> insert into dummies(name) values ('dummy1');
mysql> insert into dummies(name) values ('dummy2');
Enter fullscreen mode Exit fullscreen mode

modify routes/api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\DummyController;

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::get("data",[DummyController::class,'show']);

Enter fullscreen mode Exit fullscreen mode

run php artisan serve
Get http://127.0.0.1:8000/api/data on Postman or Browser

[
    {
        "id": 1,
        "name": "dummy1",
        "created_at": null,
        "updated_at": null
    },
    {
        "id": 2,
        "name": "dummy2",
        "created_at": null,
        "updated_at": null
    }
]
Enter fullscreen mode Exit fullscreen mode

CRUD API

CREATE

edit
app/Http/Controllers/DummyController.php

//add this to top of the code
use Symfony\Component\HttpFoundation\Response as Res;

...

    public function store(Request $request)
    {
        $dummy = Dummy::create($request->all());
        $dummy = $dummy->refresh();
        return response($dummy,Res::HTTP_CREATED);
    }
Enter fullscreen mode Exit fullscreen mode

edit app/Models/Dummy.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class dummy extends Model
{
    use HasFactory;

    protected $fillable = [
        'name'
    ];
}

Enter fullscreen mode Exit fullscreen mode

add

Route::apiResource('dummy',DummyController::class);
Enter fullscreen mode Exit fullscreen mode

(optional) Install REST client on VS code
create dummy.http

POST http://127.0.0.1:8000/api/dummy HTTP/1.1
content-type:application/json

{
    "name":"henry"
}
Enter fullscreen mode Exit fullscreen mode

click send request

//result
{
  "id": 3,
  "name": "henry",
  "created_at": "2021-02-10T04:15:28.000000Z",
  "updated_at": "2021-02-10T04:15:28.000000Z"
}
Enter fullscreen mode Exit fullscreen mode

DELETE

edit app/Http/Controllers/DummyController.php

public function destroy(dummy $dummy)
    {
        $dummy->delete();
        return response(null, RES::HTTP_NO_CONTENT);
    }
Enter fullscreen mode Exit fullscreen mode

send request in dummy.http
DELETE http://127.0.0.1:8000/api/dummy/1

UPDATE

edit app/Http/Controllers/DummyController.php

  public function update(Request $request, dummy $dummy)
    {
        $dummy->update($request->all());
        return response($dummy,RES::HTTP_OK);
    }
Enter fullscreen mode Exit fullscreen mode

send request in dummy.http

PATCH http://127.0.0.1:8000/api/dummy/2 HTTP/1.1
content-type:application/json

{
    "name":"the modify one"
}
Enter fullscreen mode Exit fullscreen mode

READ

  • read single data

edit app/Http/Controllers/DummyController.php

  public function show(dummy $dummy)
    {
        return response($dummy,RES::HTTP_OK);
    }
Enter fullscreen mode Exit fullscreen mode

send request in dummy.http

GET http://127.0.0.1:8000/api/dummy/2 
content-type:application/json
Enter fullscreen mode Exit fullscreen mode
  • read all data

edit app/Http/Controllers/DummyController.php

   public function index()
    {
        $dummy = Dummy::get();
        return response(['data' => $dummy], Res::HTTP_OK);
    }
Enter fullscreen mode Exit fullscreen mode

send request in dummy.http

GET http://127.0.0.1:8000/api/dummy
content-type:application/json
Enter fullscreen mode Exit fullscreen mode

repo: https://github.com/pcreem/basicLaravelApi

Top comments (0)