laravel 5.1错误控制器缺少参数1

时间:2016-06-24 17:21:46

标签: laravel laravel-5.1 blade

我的index.blade.php中有我的产品列表,它运行良好,现在我尝试过滤,我用我的类别和性别完成了我的菜单。

我希望展示的产品类别为" T恤"和性别="女人"但我有这个错误:

  

StoreController.php第36行中的ErrorException:缺少参数1   dixard \ HTTP \控制器\ StoreController ::产品(5)

我正在使用此链接

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

我的路线

Route::get('shop', 'StoreController@index');
Route::get('shop/{category}/{gender}','StoreController@products');

我的控制器

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'));   


    }

3 个答案:

答案 0 :(得分:1)

您可以使用命名路线。这没什么特别的。

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

您的网址:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我这样修好了:

Route::get('shop','ShopController@index');
Route::get('shop/{categoryA}','ShopController@category'); 
Route::get('shop/{categoryB}/{genderB}','ShopController@categoryGender'); 
Route::get('shop/{categoryC}/{genderC}/{slugC}','ShopController@product');

我不知道为什么,但我改变了传递给我的路线的变量,它的工作原理!