質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

1275閲覧

Laravel10で403 THIS ACTION IS UNAUTHORIZED が出ます。

Soukyuu777

総合スコア4

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2023/05/25 04:22

実現したいこと

CRUDの更新処理(UPDATEの部分です)

前提

HTML、Laravel初心者です。
Laravel10の練習で買い物リストアプリを作っております。

発生している問題・エラーメッセージ

更新処理を記述して編集ボタンを押すと下記のメッセージが出ます。
formRequestのauthorizeはtrueにしてあります。
新規でリストを登録するのはなぜかうまくいきました。

403 THIS ACTION IS UNAUTHORIZED.

該当のソースコード

・StoreShopRequest.php

php

1<?php 2 3namespace App\Http\Requests; 4 5use Illuminate\Foundation\Http\FormRequest; 6 7class StoreShopRequest extends FormRequest 8{ 9 /** 10 * Determine if the user is authorized to make this request. 11 */ 12 public function authorize(): bool 13 { 14 return true; 15 } 16 17 /** 18 * Get the validation rules that apply to the request. 19 * 20 * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string> 21 */ 22 public function rules(): array 23 { 24 return [ 25 'name' => ['required'], 26 'quantity' => ['required', 'integer'], 27 'price' => ['required', 'integer'], 28 'sum' => ['required', 'integer'], 29 ]; 30 } 31 public function attributes() 32 { 33 return[ 34 'name' => '商品名', 35 'quantity' => '個数', 36 'price' => '価格', 37 'sum' => '合計', 38 ]; 39 } 40} 41

・ルーティング

php

1<?php 2 3use App\Http\Controllers\ProfileController; 4use App\Http\Controllers\ShopController; 5use Illuminate\Support\Facades\Route; 6 7/* 8|-------------------------------------------------------------------------- 9| Web Routes 10|-------------------------------------------------------------------------- 11| 12| Here is where you can register web routes for your application. These 13| routes are loaded by the RouteServiceProvider and all of them will 14| be assigned to the "web" middleware group. Make something great! 15| 16*/ 17 18Route::get('/', function () { 19 return view('welcome'); 20}); 21 22 23Route::get('/shop', [ShopController::class, 'index'])->name('shop.index'); 24Route::get('/shop/create',[ShopController::class, 'create'])->name('shop.create'); 25Route::post('/shop/store', [ShopController::class, 'store'])->name('shop.store'); 26Route::get('/shop/{shop}/edit',[ShopController::class, 'edit'])->name('shop.edit'); 27Route::patch('/shop/{shop}',[ShopController::class, 'update'])->name('shop.update');

・ShopController.php

php

1<?php 2 3namespace App\Http\Controllers; 4 5use App\Http\Requests\StoreShopRequest; 6use App\Http\Requests\UpdateShopRequest; 7use App\Models\Shop; 8 9class ShopController extends Controller 10{ 11 /** 12 * Display a listing of the resource. 13 */ 14 public function index() 15 { 16 $shops = Shop::all(); 17 return view('shop.index', compact('shops')); 18 } 19 20 /** 21 * Show the form for creating a new resource. 22 */ 23 public function create() 24 { 25 return view('shop.create'); 26 } 27 28 /** 29 * Store a newly created resource in storage. 30 */ 31 public function store(StoreShopRequest $request) 32 { 33 $shop = Shop::create([ 34 'name' => $request->name, 35 'quantity' => $request->quantity, 36 'price' => $request->price, 37 'sum' => $request->sum, 38 ]); 39 return redirect('/shop'); 40 } 41 42 /** 43 * Display the specified resource. 44 */ 45 public function show(Shop $shop) 46 { 47 } 48 49 /** 50 * Show the form for editing the specified resource. 51 */ 52 public function edit(Shop $shop) 53 { 54 return view('shop.edit', compact('shop')); 55 56 } 57 58 /** 59 * Update the specified resource in storage. 60 */ 61 public function update(UpdateShopRequest $request, Shop $shop) 62 { 63 //$shop = Shop::where('id', $request->route('shop')); 64 $shop->update([ 65 'name' => $request->name, 66 'quantity' => $request->quantity, 67 'price' => $request->price, 68 'sum' => $request->sum, 69 ]); 70 return redirect()->route('shop.index')->with(compact('shop')); 71 } 72 73 /** 74 * Remove the specified resource from storage. 75 */ 76 public function destroy(Shop $shop) 77 { 78 // 79 } 80} 81

・edit.blade.php

php

1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>編集</title> 8</head> 9<body> 10 <form action="{{route('shop.update', $shop)}}" method="POST"> 11 @csrf 12 @method('patch') 13 <div> 14 <label for="name">商品名</label> 15 <input type="text" id="name" name="name"> 16 </div> 17 <div> 18 <label for="quantity">個数</label> 19 <input type="number" id="quantity" name="quantity"> 20 </div> 21 <div> 22 <label for="price">価格</label> 23 <input type="number" id="price" name="price"> 24 </div> 25 <div> 26 <label for="sum">合計個数</label> 27 <input type="number" id="sum" name="sum"> 28 </div> 29 <input type="submit" value="編集する"> 30 </form> 31 <a href="{{route('shop.index')}}">戻る</a> 32</body> 33</html> 34

・index.blade.php

php

1<!DOCTYPE html> 2<html lang="en"> 3 4<head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>買い物リスト</title> 9</head> 10 11<body> 12 <h1>買い物リスト</h1> 13 <table border="1"> 14 @foreach ($shops as $shop) 15 <tr> 16 <th>商品名</th> 17 <th>個数</th> 18 <th>価格</th> 19 <th>合計個数</th> 20 <th>編集</th> 21 <th>削除</th> 22 </tr> 23 <tr> 24 <td>{{ $shop->name }}</td> 25 <td>{{ $shop->quantity }}</td> 26 <td>{{ $shop->price }}</td> 27 <td>{{ $shop->sum }}</td> 28 <td><a href="{{route('shop.edit', $shop)}}">編集</a></td> 29 <td>削除</td> 30 </tr> 31 @endforeach 32 </table> 33 <a href="{{route('shop.create')}}">登録する</a> 34</body> 35 36</html> 37

試したこと

update()メソッドの上の行にwhereを使ってみるなどをしました。
あとは、update()を使わずにsave()メソッドを使うやり方もしましたがいずれも同じ403になりました。

補足情報(FW/ツールのバージョンなど)

Mac
Laravel10
PHP8.2.3

お力添えをお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

編集用のFormRequestを使っておりませんでした。(今回だとUpdateShopRequest)
新規入力と編集でそれぞれ必要なことに気づきませんでした・・

投稿2023/05/25 06:03

Soukyuu777

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問