下記のコードだと登録ボタンを押してもURLが変わりません。
デバッグ?をした所、$requestにデータが入っている所までは確認できました。
bladeファイルのform actionの指定先が間違っているのでしょうか?
blade
1<!DOCTYPE HTML> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title>ブログ</title> 6 <link rel="stylesheet" href="/css/app.css"> 7 <script src="/js/app.js" defer></script> 8</head> 9<body> 10 <br> 11 <div class="container"> 12 <div class="row"> 13 <div class="col-md-8 col-md-offset-2"> 14 <h2>商品登録</h2> 15 16 <form class="edit-form" method="POST" action="{{ route('store') }}" 17 onSubmit="return checkSubmit()"> 18 @csrf 19 20 <div class="form-group"> 21 <p>商品名</p> 22 <input type="text" name="product_name" required> 23 </div> 24 <div class="form-group"> 25 <p>メーカー</p> 26 <select name="maker"> 27 <option value="サンプル1">サンプル1</option> 28 <option value="サンプル2">サンプル2</option> 29 <option value="サンプル3">サンプル3</option> 30 </select> 31 </div> 32 <div class="form-group"> 33 <p>価格</p> 34 <input type="text" name="text" maxlength="8"> 35 </div> 36 <div class="form-group"> 37 <p>在庫数</p> 38 <input type="text" name="text" maxlength="8"> 39 </div> 40 <div class="form-group"> 41 <p>コメント</p> 42 <textarea name="kansou" rows="4" cols="40"> 43 </textarea> 44 </div> 45 <div class="form-group"> 46 <p>商品画像</p> 47 <input type="file" name="img" class="imgform"> 48 </div> 49 <button type="submit">登録</button> 50 <button onclick="location.href='/products'">戻る</button> 51 </form> 52 53 </tr> 54 </div> 55</div> 56 </div> 57 58</body> 59</html>
web
1 <?php 2 3/* 4|-------------------------------------------------------------------------- 5| Web Routes 6|-------------------------------------------------------------------------- 7| 8| Here is where you can register web routes for your application. These 9| routes are loaded by the RouteServiceProvider within a group which 10| contains the "web" middleware group. Now create something great! 11| 12*/ 13 14Route::get('/', function () { 15 return view('welcome'); 16}); 17 18Auth::routes(); 19 20Route::get('/home', 'HomeController@index')->name('home'); 21 22//商品一覧画面を表示 23Route::get('products', 'ProductsController@showList')->name('products'); 24 25//商品詳細画面を表示 26Route::get('/products/{id}', 'ProductsController@showDetail')->name('show'); 27 28//商品登録画面を表示 29Route::get('register', 'ProductsController@showRegister')->name('regi'); 30 31//商品登録 32Route::post('/store', 'ProductsController@exeStore')->name('store'); 33 34
controller
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Models\Product; 6use Illuminate\Http\Request; 7use App\Http\Requests\ProductsRequest; 8class ProductsController extends Controller 9{ 10 /** 11 * 商品一覧を表示する 12 * 13 * @return \Illuminate\Http\Response 14 */ 15 public function showList() 16 { 17 $products = Product::all(); 18 return view('products.list', ['products' => $products]); 19 } 20 21 //商品登録画面を表示する 22 public function showRegister(){ 23 return view('products.register'); 24 } 25 26 //商品を登録する 27 public function exeStore(ProductsRequest $request){ 28 $inputs = $request->all(); 29 \DB::beginTransaction(); 30 try { 31 Product::create($inputs); 32 \DB::commit(); 33 } catch(\Throwable $e) { 34 \DB::rollback(); 35 abort(500); 36 } 37 38 \Session::flash('err_msg', '商品を登録しました'); 39 return redirect(route('products')); 40 } 41 /** 42 * Display a listing of the resource. 43 * @param int $id 44 * @return \Illuminate\Http\Response 45 */ 46 public function showDetail($id) 47 { 48 49 $products = Product::find($id); 50 51 return view('products.detail', 52 ['products' => $products]); 53} 54 55 /** 56 * Show the form for creating a new resource. 57 * 58 * @return \Illuminate\Http\Response 59 */ 60 public function create() 61 { 62 // 63 } 64 65 /** 66 * Store a newly created resource in storage. 67 * 68 * @param \Illuminate\Http\Request $request 69 * @return \Illuminate\Http\Response 70 */ 71 public function store(Request $request) 72 { 73 // 74 } 75 76 /** 77 * Display the specified resource. 78 * 79 * @param \App\Models\products $products 80 * @return \Illuminate\Http\Response 81 */ 82 public function show(products $products) 83 { 84 // 85 } 86 87 /** 88 * Show the form for editing the specified resource. 89 * 90 * @param \App\Models\products $products 91 * @return \Illuminate\Http\Response 92 */ 93 public function edit(products $products) 94 { 95 // 96 } 97 98 /** 99 * Update the specified resource in storage. 100 * 101 * @param \Illuminate\Http\Request $request 102 * @param \App\Models\products $products 103 * @return \Illuminate\Http\Response 104 */ 105 public function update(Request $request, products $products) 106 { 107 // 108 } 109 110 /** 111 * Remove the specified resource from storage. 112 * 113 * @param \App\Models\products $products 114 * @return \Illuminate\Http\Response 115 */ 116 public function destroy(products $products) 117 { 118 // 119 } 120} 121
あなたの回答
tips
プレビュー