Laravel 5.6 如何覆盖laravel资源路由?
我认为这篇文章解决不了我的问题 正常的资源路径是“索引”显示所有项目。我想做的是让“索引”显示特定id的所有相关项 因此,当我从列表中选择一个教室时,我希望我正在调用的索引操作,作为索引函数,显示该特定教室的所有人员 所以我替换了默认的资源路由Laravel 5.6 如何覆盖laravel资源路由?,laravel-5.6,Laravel 5.6,我认为这篇文章解决不了我的问题 正常的资源路径是“索引”显示所有项目。我想做的是让“索引”显示特定id的所有相关项 因此,当我从列表中选择一个教室时,我希望我正在调用的索引操作,作为索引函数,显示该特定教室的所有人员 所以我替换了默认的资源路由 //Route::resources(['attendees' => 'attendeesController']); 与 在我的控制器中,我有: public function index(Request $request,$id) {
//Route::resources(['attendees' => 'attendeesController']);
与
在我的控制器中,我有:
public function index(Request $request,$id)
{
dd($request);
...
}
在我看来,在一个特定的教室id上,我有这个
<a href="{{route('attendees.index', ['classroom' => $data->id])}}">{{$data->Reference}}
因为您只传入了1个参数。控制器中的方法“索引”需要2个参数。您可能需要检查route.php文件。
默认情况下,索引操作需要一个
$id
,因此您可以将其设置为null
public function index(Request $request,$id = null)
此外,如果您想根据文档获取特定$id
的相关项目,URL将是Attendes/123
,它将被重定向到show
功能。因此,您还需要编辑该路线。相反,尝试将查询参数传递给索引路由,并使用查询参数可以获取相关数据。
而不是
attenders/123
将是attenders?id=123
查询参数设置为显示相关项,否则显示索引。
如果您仍然希望通过索引来实现它,则需要按如下所示更改路由
Route::resource('attendees', 'AttendeesController',['only' => ['index', 'create', 'store']]);
Route::get('/attendees/{id}', 'AttendeesController@index');
请您向我解释一下
attenders/123
和attenders之间的区别?id=123
如下所示,因为您需要使用查询参数显示特定id的相关项目(过滤类型)。
public function index(Request $request,$id = null)
Route::resource('attendees', 'AttendeesController',['only' => ['index', 'create', 'store']]);
Route::get('/attendees/{id}', 'AttendeesController@index');