You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel表单提交至数据库报错,寻求解决方案

Laravel表单提交数据库报错求助

我是Laravel新手,尝试将表单数据提交到数据库时出现错误,不清楚原因。执行dd($REQUEST->all())可以正常获取到表单数据,附上相关代码及截图:

InventoryController代码

<?php

namespace App\Http\Controllers;
use App\Inventory;
use Illuminate\Http\Request;

class InventoryController extends Controller
{
  public function index(){
    return view('invetory.index');
}
  public function sales(){
    return view('invetory.sale');
}
  public function create(Request $REQUEST){
    
// dd($REQUEST->all());
inventories::Create($REQUEST->all());
    
}
}

web.php路由代码

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('/inventory', 'App\Http\Controllers\InventoryController@index')->name('map');
Route::get('/inventory/sales', 'App\Http\Controllers\InventoryController@sales');


Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/inventory', 'App\Http\Controllers\InventoryController@create')->name('invetory.create');

错误截图

错误截图
截图显示错误信息:Class 'App\Http\Controllers\inventories' not found

表单截图

表单截图
截图展示了用于提交数据的表单界面,包含对应输入字段


问题解决步骤

  1. 修正模型类名大小写:在create方法中,将inventories::Create($REQUEST->all())改为Inventory::create($REQUEST->all())。原因是引入的模型是App\Inventory(首字母大写单数),且Laravel的create方法为小写开头。

  2. 设置模型可填充字段:打开app/Inventory.php模型文件,添加$fillable属性,允许批量赋值,示例:

protected $fillable = ['product_name', 'quantity', 'price']; // 替换为你表单中的实际字段名
  1. 修正路由命名拼写(可选但建议):路由名称invetory.create存在拼写错误(少了字母n),建议改为inventory.create,同时确保表单提交时使用正确的路由名。

内容的提问来源于stack exchange,提问作者vivek kn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:57:26