リソースコントローラー
リソースコントローラーとは作成、読み取り、更新、削除を簡単に構築するためのコントローラーで、今まで一から作成していたコントローラーのメソッドのひな型を一部自動で作成できるコントローラーです。以下のコマンドで作成できます。ここでは、PlanControllerを作成します。
php artisan make:controller PlanController --resource
上記のコマンドを実行すると、app/Http/ControllersにPlanController.phpが作成されます。生成されたファイルの中身は以下のようになっています。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PlanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
このコントローラーにはデータベースの操作に必要なメソッドが既に定義されています。この定義されているメソッドを利用してデータベースの操作を行っていきたいと思います。
モデルとマイグレーションの作成
次にモデルとマイグレーションを作成します。以下のコマンドで、モデルファイルとマイグレーションファイルを作成します。
php artisan make:model Plan -m
今回作成するPlanモデルは以下のようになります。
カラム名 | 内容 |
name | プランの名前 |
上記の構造をマイグレーションファイルに記述すると以下のようになります。
Schema::create('plans', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->timestamps();
});
モデルファイルは以下のコードを追加します。
protected $fillable = [
"name",
];
}
以下のコマンドでマイグレーションを実行します。
php artisan migrate
コントローラーのメソッドの実装
以下のようにPlanControllerのメソッドを定義します。ビューファイルはresources/views/planフォルダに保存します。
<?php
namespace App\Http\Controllers;
use App\Models\Plan;
use Illuminate\Http\Request;
class PlanController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$plans = Plan::all();
return view("plan.index", compact("plans"));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view("plan.create");
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
Plan::create([
"name" => $request->input("name"),
]);
return redirect(route("plan.show"));
}
/**
* Display the specified resource.
*/
public function show(Plan $plan)
{
return view("plan.show", compact("plan"));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Plan $plan)
{
return view("plan.edit", compact("plan"));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Plan $plan)
{
$plan->update([
"name" => $request->input("name"),
]);
return redirect(route("plan.show"));
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Plan $plan)
{
$plan->delete();
return redirect(route("plan.show"));
}
}
plan.indexはplanファイルにあるindex.blade.phpを示しています。次にビューファイルを作成します。ビューファイルは一覧を表示するindex.blade.php、詳細画面を表示するshow.blade.php、新規登録画面を表示するcreate.blade.php、更新画面を表示するedit.blade.phpの4種類です。画面は以前解説したので割愛します。
次にルーティングを追加します。web.phpに以下のように記述することで、一括でルーティングを指定できます。
Route::resource("plan", PlanController::class);
上記のルーティングは以下のようになります。
メソッド名 | HTTPメソッド | URIパターン | 名前 |
index | Get | /plan | plan.index |
create | Get | /plan/create | plan.create |
store | Post | /plan | plan.store |
show | Get | /plan/{plan} | plan.show |
edit | Get | /plan/{plan}/edit | plan.edit |
update | Put/Patch | /plan/{plan} | plan.update |
destroy | Delete | /plan/{plan} | plan.destroy |
これで一括でルーティングを設定することができました。
今回は一括でルーティングを指定できるリソースコントローラーについて学びました。次回はブラウザから入力された値を検証するバリデーションについて解説します。