The registration process does not work with laravel.

Asked 2 years ago, Updated 2 years ago, 391 views

After entering characters using the store mezzot, when I press the registration button, I want to do the registration process and add it to the list screen, but I got the following error, so it doesn't work very well.Please let me know.

error messages:

SQLSTATE [HY000]:General error: 1364 Field 'product_name' does not have a default value (SQL: insert into 'products' (`updated_at', `created_at') values (2022-06-212:32:24, 2022-06-2122:32:24))

Product information registration screen:

Product Information Registration Screen

Source Code

<?php
use Illuminate\Support\Facades\Route;
useApp\Http\Controllers\productsController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider with 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 extensions Controller
{
    public function index()
    {
        $query=Products::query();
        // Get all cases
        // $users=$query->get();
        // Page Nation
        $products=$query->orderBy('id', 'desc')->paginate(10);
        return view('products.index') ->with('products',$products);
    }

    public function create()
    {
        //Forward to create
        return view('products.create');
    }

    public function store(Request$request)
    {
        $products=Products::create();

        // Register Value
        $products->product_name = $request->product_name;

        // Save
        $products->save();

        // Redirect to List
        return redirect()->to('/products');
    }
}

create.blade.php

@extends('layouts.app')

@section('content')

<h1>Product Information Registration Screen</h1>

<div class="row">
        <div class="col-sm-12">
            <a href="{{route('products.index')}}" class="btn btn-primary" style="margin:20px;">Return to list</a>
        </div>
    </div>

    <!--form-->
    <form method="post" action="{{route('products.store')}}">

        <div class="form-group">
            <label>Product Name</label>
            <input type="text" name="product_name" value="class="form-control">
        </div>


        <input type="hidden" name="_token" value="{{csrf_token()}}}">

        <input type="submit" value="registration" class="btn btn-primary">

    </form>

@stop

index.blade.php

@extends('layouts.app')

@section('content')

<h1>Product Information List Screen</h1>

<a href="{{route('products.create')}}">New Registration</a>

<table class="table table-striped">
 <thead>
  <tr>
   <th>id</th>
   <th>Product Images</th>
   <th>Product Name</th>
   <th>Price</th>
   <th>Inventory </th>
   <th> Manufacturer </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">Detailed view</a></td>
   <td><Button type="button" class="btn btn-danger">Delete</button></td>
  </tr>
  @endforeach
 </tbody>
<table>

php laravel

2022-09-30 22:04

1 Answers

https://qiita.com/seltzer/items/3096f0805440bfa19bff
Have you tried this?It's good to change the setting STRICT_TRANS_TABLES


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.