簡體   English   中英

Laravel 5.3將參數從視圖傳遞到控制器

[英]Laravel 5.3 passing parameter from view to controller

我正在網上商店,所以有產品。 我正在輸出所有產品圖像及其名稱,當用戶單擊產品以將其重定向到單個產品頁面時,我想要將產品ID傳遞到單個產品視圖的問題是我想要的。

這是我的代碼路由:

Route::get('single', [
    "uses" => 'ProductsController@single',
    "as" => 'single'
]);

Index.blade.php:

<a href="{{ route('single', $product->product_id) }}" class="link-product-add-cart">See product</a>

和控制器:

public function single($product_id)
{
    $product = Product::where('product_id', $product_id);

    return view('single-product', compact("product"));
}

如下更改路線:

Route::get('single/{product_id}', [
    "uses" => 'ProductsController@single',
    "as" => 'single'
]);

如果要將任何參數傳遞給路由,則必須為參數分配占位符,因此,在這種情況下, {product_id}將用作占位符,用於從URI中獲取參數,例如: http://example.com/single/1 : http://example.com/single/1 因此,您將在單個方法中收到1作為$product_id

您需要捕獲路由中的URI段。

Route::get('single/{id}', [
    "uses" => 'ProductsController@single',
    "as" => 'single'
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM