LoginSignup
1
1

More than 1 year has passed since last update.

Laravel リソースコントローラ

Posted at

備忘録

リソースコントローラとは

簡単に言うと、CRUD処理を持つコントローラを楽に作成できる機能です。

作成

php artisan make:controller 〇〇Controller --resource

上記のように、コントローラ作成のコマンドに" --resource "オプションを付けることで作成できます。

コントローラの中身

実際に作成してみると、以下の様なコントローラができます。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

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

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

また引数のid部分を、ルートモデル結合で他モデルを引数で与えたいときは、

php artisan make:controller 〇〇Controller --model=△△ --resource

のように記述すると、$idの部分が△△モデルになって作成されます。

ルーティング

さらにルーティング記述もかなり楽になります。

web.php
Route::resource('users', UserController::class);

上のルーティングを記述するだけで、以下のルーティングをしてくれます。
なんならルート名まで定義してくれます。

動詞 URI アクション ルート名
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT/PATCH /users/{user} update users.update
DELETE /users/{user} destroy users.destroy

(Laravelドキュメント よりほぼ引用)

また、特定のルーティングのみにしたい、除きたいときは以下のように記述すれば可能です。

web.php
Route::resource('users', UserController::class)->only([
    'index', 'show' //indexとshowのみ
]);

Route::resource('users', UserController::class)->except([
    'create', 'store', 'update', 'destroy' //create,store,update,destroyは除く
]);

最後に

他にも掲示板リソースがチャットリソースを複数持っているなどの、
あるリソースが複数のリソースを持つ場合のルーティングも簡潔に済ますことが可能だったりします。
詳しくはLaravelドキュメントを参照してください。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1