Reputation: 89
I have error:
ArgumentCountError Too few arguments to function App\Http\Controllers\ProjectController::index(), 0 passed in C:\xampp\htdocs\ContentBaseApp\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
In my ProjectController.php
How can i solve this
This is my ProjectController.php
public function index($product_id)
{
// $projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->latest()->paginate(20);
$product = Product::where('user_id', Auth::id())->where('id', $product_id)->firstOrFail();
$projects = Project::where('product_id', $product->id)->latest()->paginate(20);
return view('projects.index', compact('projects'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
I wrote index method code because I want; suppose I have two id 1
& 2
if I click id 1
it will take me to index.blade.php
where I can upload new data like data_1
and data_2
those data will show on index.blade.php
And if I click id 2
it will take me to index.blade.php
where I don't want to see data of id 1
as I uploaded data1 and data2.
If I upload new data for id 2
I can see those data in index.blde.php
Upvotes: 0
Views: 14486
Reputation: 10210
The index
function on your ProjectController
expects one argument ($product_id
) as per the error message you're seeing.
Whenever you access of reference the index
route, it needs to be provided with the $product_id
parameter otherwise an error will be thrown (the one you're seeing).
If you're following convention, what you likely want to do is define two routes:
index
route that displays all resources
and takes zero argumentsshow
route that displays information for a single resource
and requires one argument which identifies a resource
.For example:
web.php
Route::get('/projects', [ProjectController::class, 'index')
->name('projects.index');
Route::get('/projects/{product}', [ProjectController::class, 'show'])
->name('projects.show');
ProjectController.php
public function index()
{
return view('projects.index', [
'projects' => Project::paginate(20),
]);
}
public function show(Product $product)
{
return view('projects.show', [
'projects' => $product->projects()->latest()->paginate(20),
]);
}
If you visit /projects
you will return all project
resources, but if you visit /projects/2
you will get only the projects
related to Product
with id
of 2
.
You can use the route()
helper to generate your URLs
.
<a href="{{ route('projects.show', 2) }}"> ... </a>
That would create a link that would take you to the projects
for product
2
.
Upvotes: 2