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
表单截图

截图展示了用于提交数据的表单界面,包含对应输入字段
问题解决步骤
修正模型类名大小写:在
create方法中,将inventories::Create($REQUEST->all())改为Inventory::create($REQUEST->all())。原因是引入的模型是App\Inventory(首字母大写单数),且Laravel的create方法为小写开头。设置模型可填充字段:打开
app/Inventory.php模型文件,添加$fillable属性,允许批量赋值,示例:
protected $fillable = ['product_name', 'quantity', 'price']; // 替换为你表单中的实际字段名
- 修正路由命名拼写(可选但建议):路由名称
invetory.create存在拼写错误(少了字母n),建议改为inventory.create,同时确保表单提交时使用正确的路由名。
内容的提问来源于stack exchange,提问作者vivek kn
相关产品推荐
相关产品推荐

