laravel filter if else语句不起作用

时间:2017-08-29 07:51:51

标签: php laravel laravel-5.4

我试图过滤我的产品价格性别颜色 这是我的工作代码,当我使用像url localhost / category / tag?price = 100

if (request()->has('gender')) {
     $products = Product::withAllTags($tags)->where('gender', request('gender'))->get();
  }
  if (request()->has('price')) {
     $products = Product::withAllTags($tags)->where('price', '<=', request('price'))->get();
  }
  if (request()->has('color')) {
     $products = Product::withAllTags($tags)->whereHas('colors', function ($query) {
          $query->where('name', request('color'));
      })->paginate(20);
  }
  if (request()->has('brand')) {
     $products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
          $query->where('name', request('brand'));
      })->orderBy('created_at', 'desc')->paginate(20);
  }

但是当url是localhost / category / tag

时会收到错误
Undefined variable: products

我尝试添加其他条件

if (request()->has('gender')) {
     $products = Product::withAllTags($tags)->where('gender', request('gender'))->get();
  }
  if (request()->has('price')) {
     $products = Product::withAllTags($tags)->where('price', '<=', request('price'))->get();
  }
  if (request()->has('color')) {
     $products = Product::withAllTags($tags)->whereHas('colors', function ($query) {
          $query->where('name', request('color'));
      })->paginate(20);
  }
  if (request()->has('brand')) {
     $products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
          $query->where('name', request('brand'));
      })->orderBy('created_at', 'desc')->paginate(20);
  }

  else  {
     $products = Product::withAllTags($tags)->orderBy('created_at', 'desc')->paginate(20);
    }  

它在localhost / category / tag上工作 但现在过滤器不能在localhost / category / tag上工作?price = 100 所有数据都显示对不起我的语法

1 个答案:

答案 0 :(得分:2)

你基本上在做

if (request()->has('brand')) {
     $products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
          $query->where('name', request('brand'));
      })->orderBy('created_at', 'desc')->paginate(20);
  }

  else  {
     $products = Product::withAllTags($tags)->orderBy('created_at', 'desc')->paginate(20);
    }  

使else语句适用于request()->has('brand') 将所有条件包装在if语句中,其余条件包含在else语句中

if(request()->has('gender')||request()->has('price')||request()->has('color')||request()->has('brand')){
if (request()->has('gender')) {
     $products = Product::withAllTags($tags)->where('gender', request('gender'))->get();
  }
  if (request()->has('price')) {
     $products = Product::withAllTags($tags)->where('price', '<=', request('price'))->get();
  }
  if (request()->has('color')) {
     $products = Product::withAllTags($tags)->whereHas('colors', function ($query) {
          $query->where('name', request('color'));
      })->paginate(20);
  }
  if (request()->has('brand')) {
     $products = Product::withAllTags($tags)->whereHas('brands', function ($query) {
          $query->where('name', request('brand'));
      })->orderBy('created_at', 'desc')->paginate(20);
  }
}
  else  {
     $products = Product::withAllTags($tags)->orderBy('created_at', 'desc')->paginate(20);
    }