我不认为这篇文章如何覆盖 laravel 资源路由默认方法?解决了我的问题。
正常的资源路由是“索引”显示所有项目。我想要做的是让“索引”显示特定 ID 的所有相关项目。
因此,当我从列表中选择教室时,我希望我正在调用的索引操作显示该特定教室的所有人,因为它是索引功能。
所以我替换了默认的资源路由
//Route::resources(['attendees' => 'attendeesController']);
和
Route::resource('attendees', 'attendeesController')->names([
'index' => 'attendees.index',
'store' => 'attendees.store',
'create' => 'attendees.create',
'show' => 'attendees.evaluation',
'update' => 'attendees.update',
'destroy' => 'attendees.destroy',
'edit' => 'attendees.edit',
]);
所以在我的控制器中,我有这个:
public function index(Request $request,$id)
{
dd($request);
...
}
在我看来,在特定的教室 ID 上,我有这个
<a href="{{route('attendees.index', ['classroom' => $data->id])}}">{{$data->Reference}}
那为什么我会得到这个?我猜一些非常基本的东西,但我看不出是什么。
Type error: Too few arguments to function
App\Http\Controllers\AttendeesController::index(),
1 passed and exactly 2 expected
默认情况下,索引操作需要一个,$id
因此您可以将其设置为 null
public function index(Request $request,$id = null)
此外,如果您想$id
根据文档 URL获取特定项目的相关项目,那么attendees/123
它将被重定向到show
函数。因此,您还需要编辑该路线。而不是尝试将查询参数传递给索引路由并使用查询参数,您可以获得相关数据。而不是attendees/123
它会attendees?id=123
查询参数设置为显示相关项,否则显示索引。如果您仍然想通过索引实现它,则需要更改如下路线
Route::resource('attendees', 'AttendeesController',['only' => ['index', 'create', 'store']]);
Route::get('/attendees/{id}', 'AttendeesController@index');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句