I'm having a problem with the optional parameter; in particular I notice that it is not read in the link.
I explain in detail: in the view I have a table with several clients. In this table there is a column with the active projects for each customer (through the CLient_id foreign key present in the Projects table).
So what I want is that when I click on that client's number of active projects, it shows me these active projects.
ROUTE:
Route::get('/project/index/{id?}', [ProjectController::class, 'index'])->name('project.index');
ProjectController:
public function index($id = NULL)
{
if($id){
$projects = Project::where('client_id', $id)->get();
} else {
$projects = Project::all();
}
return view('project.index', compact('projects'));
}
View:
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>N. active projects</th>
</tr>
@foreach ($clients as $client)
<tr>
<td>{{ $client->id }}</td>
<td>{{ $client->name }}</td>
<td>{{ $client->email }}</td>
<td>{{ $client->address }}</td>
<td><a href="{{route('project.index')}}" class="link-primary">{{ $client->projects_count }}</a></td>
<td>
When I click on the number of active projects of that client, it always takes me back to the index where ALL the projects are present.
The parameter is working as expected. Change
<td><a href="{{route('project.index')}}" class="link-primary">{{ $client->projects_count }}</a></td>
to
<td><a href="{{route('project.index', ['id' => $client->id])}}" class="link-primary">{{ $client->projects_count }}</a></td>
to actually pass the $id
to the controller.