🚀

サクッと作れるLaravel 11 で超簡単な RESTful API を実装!!

2024/05/09に公開

導入

"バリデーションや例外処理は通常必要ですが、今回は API の実装手順に焦点を当て、手順を簡潔に紹介します。"

Setting up the API

php artisan install:api

Create Model Factory Migration Seeder

php artisan make:model Todo -mfs
# INFO Model [app/Models/Todo.php] created successfully.
# INFO Factory [database/factories/TodoFactory.php] created successfully.
# INFO Migration [database/migrations/create_todos_table.php] created successfully.
# INFO Seeder [database/seeders/TodoSeeder.php] created successfully.

Table

create_todos_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('todos', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('todos');
    }
};
php artisan migrate

Controller

php artisan make:controller TodoController --resource

Post

TodoController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use app\Models\Todo;

class TodoController extends Controller
{
    public function store(Request $request) {
        $todo = Todo::create($request->only('title'));
        return response()->json(['todo' => $todo]);
    }
}
Models\Todo.php
<?php

namespace App\Models;

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

class Todo extends Model
{
    use HasFactory;

    protected $fillable = ['title'];
}
routes\api.php
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::apiResource('todos', \App\Http\Controllers\TodoController::class);
Request
// POST:http://localhost:8080/api/todos
{
  "title": "test"
}

image

Get

TodoController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use app\Models\Todo;

class TodoController extends Controller
{
  public function index() {
      $todos = Todo::all();
      return response()->json(['todos' => $todos]);
  }
}
Request
GET:http://localhost:8080/api/todos

image

PUT

TodoController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use app\Models\Todo;

class TodoController extends Controller
{
    public function update(Request $request, $id) {
        $todo = Todo::findOrFail($id);
        $todo->update($request->only('title'));
        return response()->json(['todo' => $todo]);
    }
}
Request
// PUT:http://localhost:8080/api/todos
{
  "title": "アップデート処理"
}

image

Delete

TodoController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use app\Models\Todo;

class TodoController extends Controller
{
    public function update(Request $request, $id) {
        $todo = Todo::findOrFail($id);
        $todo->delete();
        return response()->json(null, 204);
    }
}
Request
Delete:http://localhost:8080/api/todos/1
GitHubで編集を提案

Discussion