storeメゾットを使って文字を入力後、登録ボタンを押して登録処理をして一覧画面に追加表記させたいと思っているのですが、なかなかうまくいきません。
ご教授いただきたいと思います。
Not Found
The requested URL was not found on this server.
このようなエラーになります。
他に追加の情報も対応します。
web.php
1<?php 2use Illuminate\Support\Facades\Route; 3use App\Http\Controllers\productsController; 4 5/* 6|-------------------------------------------------------------------------- 7| Web Routes 8|-------------------------------------------------------------------------- 9| 10| Here is where you can register web routes for your application. These 11| routes are loaded by the RouteServiceProvider within a group which 12| contains the "web" middleware group. Now create something great! 13| 14*/ 15 16Route::get('/', function () { 17 return view('welcome'); 18}); 19 20Auth::routes(); 21 22Route::get('/home', 'HomeController@index')->name('home'); 23Route::get('products','ProductsController@index'); 24Route::get('products/create','ProductsController@create'); 25Route::post('products/store','ProductsController@store');
productsController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Models\products; 7 8class productsController extends Controller 9{ 10 public function index() 11 { 12 $query = Products::query(); 13 //全件取得 14 //$users = $query->get(); 15 //ページネーション 16 $products = $query->orderBy('id','desc')->paginate(10); 17 return view('products.index')->with('products',$products); 18 } 19 20 public function create() 21 { 22 //createに転送 23 return view('products.create'); 24 } 25 26 public function store(Request $request) 27 { 28 $products = Products::create(); 29 30 //値の登録 31 $products->product_name = $request->product_name; 32 33 //保存 34 $products->save(); 35 36 //一覧にリダイレクト 37 return redirect()->to('/products'); 38 } 39}
create.blade.php
1@extends('layouts.app') 2 3@section('content') 4 5<h1>商品情報登録画面</h1> 6 7<div class="row"> 8 <div class="col-sm-12"> 9 <a href="/step/public/products" class="btn btn-primary" style="margin:20px;">一覧に戻る</a> 10 </div> 11 </div> 12 13 <!-- form --> 14 <form method="post" action="/products/store"> 15 16 <div class="form-group"> 17 <label>商品名</label> 18 <input type="text" name="product_name" value="" class="form-control"> 19 </div> 20 21 22 <input type="hidden" name="_token" value="{{csrf_token()}}"> 23 24 <input type="submit" value="登録" class="btn btn-primary"> 25 26 </form> 27 28@stop
index.blade.php
1@extends('layouts.app') 2 3@section('content') 4 5<h1>商品情報一覧画面</h1> 6 7<a href="/step/public/products/create">新規登録</a> 8 9<table class="table table-striped"> 10 <thead> 11 <tr> 12 <th>id</th> 13 <th>商品画像</th> 14 <th>商品名</th> 15 <th>価格</th> 16 <th>在庫数</th> 17 <th>メーカー名</th> 18 </tr> 19 </thead> 20 <tbody> 21 @foreach ($products as $products) 22 <tr> 23 <td>{{ $products->company_id }}</td> 24 <td>{{ $products->img_path }}</td> 25 <td>{{ $products->product_name }}</td> 26 <td>{{ $products->price }}</td> 27 <td>{{ $products->stock }}</td> 28 <td>{{ $products->comment }}</td> 29 <td><a href="" class="btn btn-primary">詳細表示</a></td> 30 <td><button type="button" class="btn btn-danger">削除</button></td> 31 </tr> 32 @endforeach 33 </tbody> 34<table>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。