从视图中的何处获取路由的第二个参数?



My App围绕一些模型发展,包括UserCollection。在这些之间存在着一个(用户(对多个(集合(的关系。

在我的collections.create视图中,我想将操作属性设置为如下所示的路由:

<form action="{{ route('users.collections.store') }}" method="POST">

我知道路由函数中应该有第二个参数,但我不知道从哪里得到它。

我的路线是:

Route::resource('users.collections', 'UserCollectionController');
Route::resource('users', 'UserController');

您可以像下面这样传递参数:

Route::get('user/{id}', function ($id) { // $id would be the parameter in this case
return 'User '.$id;
});

还有可选参数,您不需要"填充",但如果需要,可以

Route::get('user/{name?}', function ($name = null) {
return $name;
});

基本上,一个只是添加一个?通配符。

在边栏选项卡中,可以添加如下所示的参数:

<form action="{{ route('users.collections.store', [('user' => $var->user)] }}" method="POST">

要了解更多信息,文档中有关于路由参数的部分

如果您尝试在UserCollectionController上点击存储方法,您可以执行以下操作:

首先,在刀片视图中,您可以像这样将许多参数传递给您的路由:

<form action="{{ route('users.collections.store', ['someVariable' => $collection->someProperty, 'anotherVariable' => $user->anotherProperty])) }}" method="POST">

因此,现在您只需编辑 routes 文件即可应用这些更改,但您将覆盖已创建的资源控制器方法,因此如下所示(顺便说一句,代码中的路由未命名(:

Route::post('someWebPage/{someProperty}/{anotherProperty}', 'UserCollectionController@store')->name('users.collections.store');

在你的UserCollectionController.php当然,你可以这样接受它们:

public function store(Request $request, $someProperty, $anotherProperty) { //Of course here you don't have to stick to the naming i'm just trying to make it clear
// $collection = AppCollection::where('aProperty', $someProperty)-get();
}

我希望这是你想要的,希望它有所帮助。

最新更新