29
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-03-26

Resource & route()

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

ルーティング

web.php

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

制限する時

web.php
Route::resource('user', 'UserController', ['only' => ['index', 'show']]);

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", $user_id) }}">詳細画面へのリンク</a>

edit(編集画面)

リンク

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

update(更新処理)

form

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

delete(削除処理)

form

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

Controller

UserController

UserController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

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

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

    public function store(Request $request)
    {
        User::create($request->all());
    }

    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)
    {
        User::where('id', $id)->update($request->all());
    }

    public function destroy($id)
    {
        User::where('id', $id)->delete();
    }
}
29
24
1

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
29
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?