Laravel 5.3 将参数从视图传递到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40472230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.3 passing parameter from view to controller
提问by Angel Miladinov
I am making an online shop, so it has products. I am outputing all the products images and their names and I want when the user clicks on the product to redirect him to a single-product page the problem I have is passing the id of the product to the single-product view.
我正在做一个网上商店,所以它有产品。我正在输出所有产品图像及其名称,我希望当用户单击产品将他重定向到单一产品页面时,我遇到的问题是将产品的 id 传递给单一产品视图。
Here's my code Routing:
这是我的代码路由:
Route::get('single', [
"uses" => 'ProductsController@single',
"as" => 'single'
]);
Index.blade.php:
索引.blade.php:
<a href="{{ route('single', $product->product_id) }}" class="link-product-add-cart">See product</a>
And the controller:
和控制器:
public function single($product_id)
{
$product = Product::where('product_id', $product_id);
return view('single-product', compact("product"));
}
采纳答案by Rimon Khan
回答by The Alpha
Make changes to your route as given below:
对您的路线进行如下更改:
Route::get('single/{product_id}', [
"uses" => 'ProductsController@single',
"as" => 'single'
]);
If you want to pass any parameters to your route then you've to assign placeholders for the parameters, so in your case, the {product_id}
will be used as the placeholder which will be used to take the parameter from the URI
, for example: http://example.com/single/1
. So, you'll receive the 1
as $product_id
in your single method.
如果要将任何参数传递给路由,则必须为参数分配占位符,因此在您的情况下,{product_id}
将用作占位符,用于从 中获取参数URI
,例如:http://example.com/single/1
。因此,您将在单一方法中收到1
as $product_id
。