我創建了一個使用播種機生成的資料庫,但是現在我希望用戶輸入資料并允許我在螢屏上看到它并將其添加到資料庫中。
當我輸入資料并點擊提交時,我也會收到此錯誤。它說:
ArgumentCountError 函式 App\Http\Controllers\InventoryController::store() 的引數太少,1 在第 54 行傳入 /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php 正好是 2預期的
http://localhost/inventories
這是我的路由器 web.php:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', [\App\Http\Controllers\HomeController::class, 'index'])->name('pages.index');
Route::get('/inventories', [\App\Http\Controllers\InventoryController::class, 'index'])->name('index');
Route::get('/inventories/create',[\App\Http\Controllers\InventoryController::class, 'create']);
Route::post('/inventories',[\App\Http\Controllers\InventoryController::class,'store']);
這是我的控制器 InventoryController.php:
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;
/**
* Class InventoryController
*/
class InventoryController extends Controller
{
/**
* Get the list of inventories.
*
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
$inventories = Inventory::all();
return view('pages.inventories',[
"inventories" => $inventories
]);
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function create()
{
return view('pages.inventories.create');
}
/**
* @param StorePostRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request, $inventory)
{
$validated = $request->validate();
$request->validate([
'title'=> 'required|string',
'description'=> 'required|value:300',
'price' => 'required|integer|min:0',
'in_stock' => 'required|integer',
'on_sale' => 'required|boolean'
]);
$inventory = $request->query('title');
$inventory = $request->query('description');
$inventory = $request->query('price');
$inventory = $request->query('in_stock');
$name = $request->query('on_sale');
return redirect()->route('/inventories');
}
}
這是我的 Inventory.php 模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
use HasFactory;
/**
* @var array
*/
protected $casts = [
'on_sale' => 'boolean',
];
/**
* @var string[]
*/
protected $fillable = [
'title',
'description',
'price',
'in_stock',
'on_sale',
'updated_at',
'created_at'
];
protected $guarded = [];
}
這是我的 create.blade.php 檔案,用戶可以在其中輸入資料(作業正常但點擊提交時出錯):
@extends('layouts.app')
@section('title', 'Create Inventory')
@section('content')
<h1><strong>Please fill in the blanks to create an inventory.</strong></h1>
<p><a href="/">Click here</a> to go back to the homepage.</p>
<p><a href="/inventories">Click here</a> to go to the generated inventory page.</p>
<form action="/inventories" method="post">
@csrf
<label for="title">Enter an item name:</label>
<input type="text" name="title" required /><br><br>
<label for="description">Enter the item's description:</label>
<textarea name="description" required></textarea><br><br>
<label for="price">Enter the item's price:</label>
<input type="number" name="price" required/><br><br>
<label for="in_stock">Enter a number of items in stock:</label>
<input type="number" name="in_stock" required/><br><br>
<label for="on_sale">Enter yes/no if item is on sale:</label>
<input type="text" name="on_sale" required/><br><br>
<button type="submit">Submit</button>
</form>
@endsection
任何幫助都會有用,因為我完全不知所措。我只想顯示用戶在庫存頁面上輸入的資料,然后添加一條訊息,說明它是否有效,以便用戶知道。謝謝!
uj5u.com熱心網友回復:
您的 store 方法需要將 $inventory 傳遞給它:
public function store(Request $request, $inventory)
但您的路線沒有定義名為 $inventory 的路線引數:
Route::post('/inventories',[\App\Http\Controllers\InventoryController::class,'store']);
所以錯誤告訴您傳遞給函式的引數太少(傳遞 1 個)(預期為 2 個)。所以首先洗掉額外的引數:
public function store(Request $request)
然后在開始用資料填充它之前在方法中創建一個新清單:
$inventory = new Inventory();
$inventory = $request->query('title');
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401805.html
上一篇:保存前如何最好地驗證反向關系總數
