2016-06-24 43 views
0

我有我的index.blade.php中的產品列表foreach,它工作得很好,現在我嘗試過濾,我做了我的菜單與我的類別和性別。laravel 5.1錯誤控制器缺少參數1

我想展示產品類別= 「T恤」 和性別= 「女」,但我有此錯誤:

ErrorException in StoreController.php line 36: Missing argument 1 for dixard\Http\Controllers\StoreController::products()

我使用這個鏈接

<a href="{{url('shop', ['category'=> 't-shirt', 'gender' => 'woman'])}}" title=""> 
    <span>Woman</span> 
</a> 

我的路線

Route::get('shop', '[email protected]'); 
Route::get('shop/{category}/{gender}','[email protected]'); 

我控制器

public function products($category, $gender) 
    { 


      $gender_id= Gender::where('gender', $gender)->first(); 
      $category_id= Category::where('name', $category)->first(); 
      $filter = ['gender_id' => $gender_id->id, 'category_id' => $category_id->id]; 
      $products = Product::where($filter)->orderBy('id', 'asc')->get(); 
      $categories = Category::all(); 
      return view('store.index', compact('products','categories')); 


    } 
+0

哪行在你的控制器代碼中是36? – Yani

+0

@Yani第36行 - >公共函數產品($ category,$ gender) –

回答

0

使用路由功能(https://laravel.com/docs/5.1/routing

<a href="{{route('shop', ['category'=> 't-shirt', 'gender' => 'woman'])}}" title=""> 
<span>Woman</span> 
</a> 
+0

我有這個錯誤,如果我使用路由,而不是url:「ErrorException UrlGenerator.php行306: Route [shop] not defined。(View: C:\ xampp \ htdocs \ 2016 \ resources \ views \ layouts \ principal.blade.php)(查看:C:\ xampp \ htdocs \ 2016 \ resources \ views \ layouts \ principal.blade.php)「 –

+0

'url('/ shop/t-shirt/woman')' –

+0

結果是 - >頁面沒有找到 –

1

您可以使用命名路由。這沒什麼特別的。

Route::get('shop/{category}/{gender}', [ 
    'uses' => [email protected]', 
    'as' => 'shopRoute' 
]); 

以及您的網址:

route('shopRoute', ['category'=> 't-shirt', 'gender' => 'woman']) 
+0

它沒有工作,它給我同樣的錯誤 –

+0

@ Diego182看看更新的答案 –

+0

我試了!同樣的錯誤:StoreController.php中的ErrorException第36行: 缺少dixard \ Http \ Controllers \ StoreController :: products()的參數1 –