LoginSignup
0
0

More than 1 year has passed since last update.

LaravelのResource ControllerによるCRUD操作

Last updated at Posted at 2022-06-16

ドキュメント
公式:https://laravel.com/docs/9.x/controllers#main-content
Readouble:https://readouble.com/laravel/8.x/ja/controllers.html

.bash
php artisan make:CreateController
php artisan make:DeleteController

などと記事の投稿・削除などのCRUD操作それぞれに対して自分で設定を行っていた。

が、これらを一括で作れるのがリソースコントローラー

.bash
php artisan make:PostsController --resource

これにより以下のファイルが作成できる。

PostsController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController 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)
    {
        //
    }
}

それぞれのfunctionにコードを書いていけばいいだけだ。

Routingは以下のよう。

.bash
php artisan route:list

  GET|HEAD        posts .......................................................... posts.index › PostsController@index
  POST            posts .......................................................... posts.store › PostsController@store
  GET|HEAD        posts/create ................................................. posts.create › PostsController@create
  GET|HEAD        posts/{post} ..................................................... posts.show › PostsController@show
  PUT|PATCH       posts/{post} ................................................. posts.update › PostsController@update
  DELETE          posts/{post} ............................................... posts.destroy › PostsController@destroy
  GET|HEAD        posts/{post}/edit ................................................ posts.edit › PostsController@edit

routesファイルには以下のように記述。

web.php
Route::resource('posts', PostsController::class)->only(['index', 'show']);

only, exceptなどで使いたいメソッドだけを指定すれば出来上がり!

0
0
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
0
0