storeメゾットを使って文字を入力後、登録ボタンを押すと登録処理をして一覧画面に追加表記させたいと思っているのですが、以下のようなうなエラーが出てしまい、なかなかうまくいきません。ご教授いただきたいと思います。
エラーメッセージ:
SQLSTATE[HY000]: General error: 1364 Field 'product_name' doesn't have a default value (SQL: insert into `products` (`updated_at`, `created_at`) values (2022-06-21 22:32:24, 2022-06-21 22:32:24))
商品情報登録画面:
ソースコード
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\productsController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/products', [productsController::class, 'index'])->name('products.index');
Route::get('/create', [productsController::class, 'create'])->name('products.create');
Route::post('/store', [productsController::class, 'store'])->name('products.store');
productsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\products;
class productsController extends Controller
{
public function index()
{
$query = Products::query();
//全件取得
//$users = $query->get();
//ページネーション
$products = $query->orderBy('id','desc')->paginate(10);
return view('products.index')->with('products',$products);
}
public function create()
{
//createに転送
return view('products.create');
}
public function store(Request $request)
{
$products = Products::create();
//値の登録
$products->product_name = $request->product_name;
//保存
$products->save();
//一覧にリダイレクト
return redirect()->to('/products');
}
}
create.blade.php
@extends('layouts.app')
@section('content')
<h1>商品情報登録画面</h1>
<div class="row">
<div class="col-sm-12">
<a href="{{ route('products.index') }}" class="btn btn-primary" style="margin:20px;">一覧に戻る</a>
</div>
</div>
<!-- form -->
<form method="post" action="{{ route('products.store') }}">
<div class="form-group">
<label>商品名</label>
<input type="text" name="product_name" value="" class="form-control">
</div>
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="submit" value="登録" class="btn btn-primary">
</form>
@stop
index.blade.php
@extends('layouts.app')
@section('content')
<h1>商品情報一覧画面</h1>
<a href="{{ route('products.create') }}">新規登録</a>
<table class="table table-striped">
<thead>
<tr>
<th>id</th>
<th>商品画像</th>
<th>商品名</th>
<th>価格</th>
<th>在庫数</th>
<th>メーカー名</th>
</tr>
</thead>
<tbody>
@foreach ($products as $products)
<tr>
<td>{{ $products->company_id }}</td>
<td>{{ $products->img_path }}</td>
<td>{{ $products->product_name }}</td>
<td>{{ $products->price }}</td>
<td>{{ $products->stock }}</td>
<td>{{ $products->comment }}</td>
<td><a href="" class="btn btn-primary">詳細表示</a></td>
<td><button type="button" class="btn btn-danger">削除</button></td>
</tr>
@endforeach
</tbody>
<table>