Help us understand the problem. What is going on with this article?

Laravelでresource使用してる時のroute()とかの一覧

Resource & route()

いつも書き方忘れてハマるので自分用に備忘録

Blade

index(一覧画面)

リンク

<a href="{{ route("user.index") }}">一覧画面へのリンク</a>

create(作成画面)

リンク

<a href="{{ route("user.create") }}">作成画面へのリンク</a>

store(作成処理)

form

<form action="{{ route("user.store") }}" action="post">
    @csrf

show(詳細画面)

リンク

<a href="{{ route("user.show", ["id" => $user_id]) }}">詳細画面へのリンク</a>

edit(編集画面)

リンク

<a href="{{ route("user.edit", ["id" => $user_id]) }}">詳細画面へのリンク</a>

update(更新処理)

form

<form action="{{ route("user.update", ["id" => $user_id]) }}" action="post">
    @csrf
    @method('PUT')

delete(削除処理)

form

<form action="{{ route("user.destroy", ["id" => $user_id]) }}" action="post">
    @csrf
    @method('DELETE')

Controller

UserController

UserController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        return view('user.index');
    }

    public function create()
    {
        return view('user.create');
    }

    public function store(Request $request)
    {
        // 作成処理
    }

    public function show($id)
    {
        return view('user.show', [
            'user_id' => $id
        ]);
    }

    public function edit($id)
    {
        return view('user.edit', [
            'user_id' => $id
        ]);
    }

    public function update(Request $request, $id)
    {
        // 更新処理
    }

    public function destroy($id)
    {
        // 削除処理
    }
}

ルーティング

web.php

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

制限する時

web.php
Route::resource('user', 'UserController', ['only' => ['index', 'show']]);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした