【问题标题】:Laravel filter of multiple variables from multiple models来自多个模型的多个变量的 Laravel 过滤器
【发布时间】:2020-09-03 22:22:21
【问题描述】:

早安

我正在尝试创建一个包含多个变量的过滤器,例如我想按类别过滤我的产品(例如“水果”),然后我想过滤标签(例如“销售”),因此结果我得到了所有打折的水果。我设法在 laravel 中为类别和标签编写了单独的过滤器,但如果我让它们在我的 productsController 中都处于活动状态,它们就会相互矛盾。我想我必须用 if/else 语句编写一个函数,但我不知道从哪里开始。有人可以帮我解决这个问题吗?

这些是我在 productsController 中的函数:

 public function productsPerTag($id){
    $tags = Tag::all();
    $products = Product::with(['category','tag','photo'])->where(['tag_id','category_id'] ,'=', $id)->get();
    return view('admin.products.index',compact('products','tags'));
}
public function productsPerCategory($id){ 
    $categories = Category::all(); //om het speciefieke id op te vangen heb ik alle categories nodig
    $products = Product::with(['category','tag','photo'])->where('category_id', '=', $id)->get(); 
    return view('admin.products.index',compact('products','categories'));
}

这些是我在 web.php 中的路线。我想这也必须改变:

Route::get('admin/products/tag/{id}','AdminProductsController@productsPerTag')->name('admin.productsPerTag');

Route::get('admin/products/category/{id}','AdminProductsController@productsPerCategory')->name('admin.productsPerCategory');

【问题讨论】:

  • 您的$id 是否与tag_idcategory_id 相同??

标签: laravel function controller routes filtering


【解决方案1】:

同时过滤

更改您的网址,如

    Route::get('admin/products/tag/{tag_id?}/{category_id?}','AdminProductsController@productsPerTag')->name('admin.productsPerTag');

让你的函数像控制器一样

 public function productsPerTag($tagId = null, $categoryId = null){
    $tags = Tag::all();
    $categories = Category::all();

    $query = Product::with(['category','tag','photo']);
    if ($tagId) {
        $query->where(['tag_id'] ,'=', $tagId);
    }
    if ($tagId) {
        $query->where(['category_id'] ,'=', $categoryId);
    }
    $products = $query->get();

    return view('admin.products.index',compact('products','tags', 'categories'));
}

【讨论】:

    【解决方案2】:

    如果需要任何修改,希望它能给你所有预期的结果,让我知道:

    public function productList($tag_id = null , $category_id = null){
        $tags = Tag::all();
        $categories = Category::all();
        if($tag_id && $category_id) {
            $products = Product::with(['category','tag','photo'])
                               ->where('tag_id' , $tag_id)
                               ->where('category_id' , $category_id)
                               ->get();
        } elseif($tag_id && !$category_id) {
            $products = Product::with(['category','tag','photo'])
                               ->where('tag_id' , $tag_id)
                               ->get();
        } elseif($category_id && !$tag_id) {
            $products = Product::with(['category','tag','photo'])
                               ->where('category_id' , $category_id)
                               ->get();
    
        } elseif(!$category_id && !$tag_id) {
            $products = Product::with(['category','tag','photo'])
                               ->get();
        }
        return view('admin.products.index',compact(['products','tags','products']));
    }
    

    路线:

    Route::get('admin/products/tag/{tag_id?}/{category_id?}','AdminProductsController@productsPerTag')->name('admin.productsPerTag');
    

    【讨论】:

      【解决方案3】:

      您正在尝试在查询中进行过滤,但您只将 1 个参数传递给您的控制器,这不起作用。

      1) 您需要将过滤器添加为 URL 中的查询参数,因此您的 url 将如下所示:

      admin/products/tag/1?category_id=2
      

      查询参数不能放在web.php 中。当您使用 URL 并且是可选的时,您可以像上面一样使用它们。

      2) 更改您的控制器以接受过滤器:

      public function productsPerTag(Request $request)
      {
          $categoryId = $request->input('category_id', '');
          $tags = Tag::all();
          $products = Product::with(['category', 'tag', 'photo'])
              ->where('tag_id', '=', $request->route()->parameter('id'))
              ->when((! empty($categoryId)), function (Builder $q) use ($categoryId) {
                  return $q->where('category_id', '=', $categoryId);
              })
              ->get();
      
          return view('admin.products.index', compact('products', 'tags'));
      }
      

      请记住,虽然 {id}$request->route()->parameter('id')

      查询参数被处理为$request->input('category_id'),以便在控制器中检索它们。

      【讨论】:

      • 我认为你的 when 方法中有额外的 )
      • @AkhtarMunir 不,我使用编辑器进行格式化,没有错误。
      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 2020-09-17
      相关资源
      最近更新 更多