I created a database that I have generated using a seeder, however now I wish for a user to input data and have me be allowed to see it on screen and add it to the database.
I also get this error when I input data and hit submit. It says:
ArgumentCountError Too few arguments to function App\Http\Controllers\InventoryController::store(), 1 passed in /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected
http://localhost/inventories
This is my router, 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']);
This is my Controller, 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');
}
}
This is my Inventory.php model:
<?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 = [];
}
And this is my create.blade.php file where the user can input data(works fine but get error when I hit submit):
@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
Any help would be useful as I am at a complete loss. I just wish to display the data that the user input on the inventories page and then add a message of whether it worked or didn't work so the user knows. Thank you!
CodePudding user response:
Your store method requires an $inventory to be passed to it :
public function store(Request $request, $inventory)
but your route doesn't define a route parameter called $inventory :
Route::post('/inventories',[\App\Http\Controllers\InventoryController::class,'store']);
so the error is telling you that there are too few arguments being passed (1 is passed) to the function (2 are expected). So first remove the extra parameter :
public function store(Request $request)
and then create a new inventory within the method before you start populating it with data :
$inventory = new Inventory();
$inventory = $request->query('title');