laravel larvael形式提交到数据库给出错误

a11xaf1n  于 2023-10-22  发布在  其他
关注(0)|答案(3)|浏览(98)

我是新的laravel,我试图提交表格到数据库,但我得到错误,我不知道为什么我已经添加了屏幕截图沿着与控制器,当我做dd($REQUEST->all())我得到的表格数据

<?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');

v09wglhw

v09wglhw1#

根据您的代码,控制器应该如下所示:

<?php

namespace App\Http\Controllers;
use App\Models\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){
    Inventory::Create($request->all());
  }
}
3bygqnnd

3bygqnnd2#

将库存的名称空间更改为use App\Models\Inventory;
另外,在 create**function 用途:中
Inventory::Create($request->all());

kokeuurv

kokeuurv3#

您只是忘记在控制器中导入库存模型

use App\Models\Inventory;

在代码顶部添加给定的行,用于调用其他类。

class YourController extends Controller
{
    public function create(Request $request, Inventory $inventory)
    {
        $inventory->create($request->all());
    }
}

这是函数的样子。
您也可以使用PHP扩展,如PHP IntelliSense和PHP IntelliPhense,以避免此类错误。这些扩展可以为类方法、函数等提供适当的建议。

相关问题