2016-11-07 156 views
0

我在做一個網上商店,所以它有產品。我輸出所有產品圖像及其名稱,我希望當用戶點擊產品將其重定向到單品頁面時,我遇到的問題是將產品ID傳遞到單品視圖。Laravel 5.3從視圖傳遞參數到控制器

這裏是我的代碼路徑:

Route::get('single', [ 
    "uses" => '[email protected]', 
    "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")); 
} 

回答

0

您需要在route中捕獲URI的段。

Route::get('single/{id}', [ 
    "uses" => '[email protected]', 
    "as" => 'single' 
]); 
+0

我得到 缺少[路由:單個] [URI:single/{id}]的必需參數。錯誤 –

+0

在您的視圖中傳遞參數,如下所示:'{{route('single',['id'=> $ product-> product_id])}}' –

+0

有關在路徑URL中傳遞參數的更多信息,請轉到此鏈接: https://laravel.com/docs/5.3/helpers#method-route –

0

進行更改您的路線如下:

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

如果您想將任何參數傳遞給您的路線,那麼您必須爲參數指定佔位符,因此在您的情況下,{product_id}將用作佔位符,該佔位符將用於從URI獲取參數例如:http://example.com/single/1。因此,您將以單一方法收到1作爲$product_id

+0

我得到缺少[Route:single] [URI:single/{id}]所需的參數。錯誤 –

+0

有沒有'product_id'字段可用,或者它是產品表中的其他內容? –

+0

我有桌面產品,唯一的ID鍵名爲product_id –

相關問題