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

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

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

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

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

PHP

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

Q&A

解決済

1回答

1129閲覧

登録ボタンを押してもDBにデータが保存されない

Hello.world

総合スコア10

Laravel

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

phpMyAdmin

phpMyAdminはオープンソースで、PHPで書かれたウェブベースのMySQL管理ツールのことです。

PHP

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

0グッド

0クリップ

投稿2023/01/11 23:02

登録フォームを作りました。
登録ボタンを押すとDBにデータが保存されるようにしたいのですが、押しても保存されません。
送信先が間違っているのでしょうか?

Product.php

1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Product extends Model 8{ 9 // 10 protected $table = 'products'; 11 12 protected $fillable = 13 [ 14 'image', 15 'title', 16 'price', 17 'stock', 18 'comment' 19 20 ]; 21}

register.blade.php

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="{{ 17 route('store') }}" 18 onSubmit="return checkSubmit()"> 19 @csrf 20 21 <div class="form-group"> 22 <p>商品名</p> 23 <input type="text" name="product_name" required> 24 </div> 25 <div class="form-group"> 26 <p>メーカー</p> 27 <select name="maker"> 28 <option value="サンプル1">サンプル1</option> 29 <option value="サンプル2">サンプル2</option> 30 <option value="サンプル3">サンプル3</option> 31 </select> 32 </div> 33 <div class="form-group"> 34 <p>価格</p> 35 <input type="text" name="text" maxlength="8"> 36 </div> 37 <div class="form-group"> 38 <p>在庫数</p> 39 <input type="text" name="text" maxlength="8"> 40 </div> 41 <div class="form-group"> 42 <p>コメント</p> 43 <textarea name="kansou" rows="4" cols="40"> 44 </textarea> 45 </div> 46 <div class="form-group"> 47 <p>商品画像</p> 48 <input type="file" name="img" class="imgform"> 49 </div> 50 <button type="submit">登録</button> 51 <button onclick="location.href='/products'">戻る</button> 52 </form> 53 54 </tr> 55</form> 56 </div> 57</div> 58 </div> 59 60</body> 61</html>

ProductsController.php

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 public function showRegister(){ 22 return view('products.register'); 23 } 24 25 //商品を登録する 26 public function exeStore(ProductsRequest $request){ 27 $inputs = $request->all(); 28 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 redirects(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

web.php

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

ProductsRequest.php

1<?php 2 3namespace App\Http\Requests; 4 5use Illuminate\Foundation\Http\FormRequest; 6 7class ProductsRequest extends FormRequest 8{ 9 /** 10 * Determine if the user is authorized to make this request. 11 * 12 * @return bool 13 */ 14 public function authorize() 15 { 16 return true; 17 } 18 19 /** 20 * Get the validation rules that apply to the request. 21 * 22 * @return array 23 */ 24 public function rules() 25 { 26 return [ 27 'title' => 'required', 28 'price', 29 'stock', 30 'comment' 31 ]; 32 } 33} 34

create_productslist.php

1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateProductslist extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 if (!Schema::hasTable('products')) { 17 Schema::create('products', function (Blueprint $table) { 18 $table->bigIncrements('id'); 19 $table->binary('image'); 20 $table->string('title'); 21 $table->string('price'); 22 $table->string('stock'); 23 $table->string('comment'); 24 $table->timestamps(); 25 }); 26 } 27 } 28 29 /** 30 * Reverse the migrations. 31 * 32 * @return void 33 */ 34 public function down() 35 { 36 Schema::dropIfExists('productslist'); 37 } 38} 39

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

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

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

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

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

m.ts10806

2023/01/11 23:49

デバッグしてみましたか?
phper.k

2023/01/12 02:04

ProductsRequest.php がおかしい
odataiki

2023/01/12 04:46

質問文読みましたが「やってほしいことだけを記載した丸投げの質問」という指摘も致し方なしと感じました。 他の方も指摘があるようにデバッグを行いましたでしょうか? デバッグがわからない場合はデバッグ方法を先に調べることをお勧めします。 (以下デバッグができるという前提で) 今の質問文の内容からアドバイスを受けても的はずれになる可能性が高いということです。 (的に当たることもあると思いますが) 的はずれな回答アドバイスをもらっても質問者さんもがっかりするだけでしょう。 「押しても保存されません」という表現ではなく、 デバッグしてどこまで自分の意図したデータが来ているか、 どこで躓いているかを突き止めましょう。
Hello.world

2023/01/12 07:27

皆様回答ありがとうございます。 デバッグして何が原因なのか突き止めてから再度質問いたします。
m.ts10806

2023/01/12 08:09 編集

質問は編集できます。調査した結果、解決しなければ追記して回答を募集し、回答すればそこまでの顛末を回答にしてください。 「デバッグします」だけでは解決はおろか回答になってないと思います。
guest

回答1

0

自己解決

デバッグして何が原因なのか突き止めてから再度質問いたします。

投稿2023/01/12 07:27

Hello.world

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問