【问题标题】:laravel filter if else statement is not working如果 else 语句不起作用,laravel 过滤器
【发布时间】:2018-02-06 13:58:38
【问题描述】:

我试图过滤我的产品的价格性别颜色 当我使用这样的 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

我尝试添加这样的 else 条件

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 上不起作用 所有数据都显示对不起我的语法

【问题讨论】:

  • 未定义变量在哪一行和哪一页上?
  • 未定义变量:f3407ad99fc10e7e2c6983562937345f02f5bf88.php(第 84 行)中的产品(查看:C:\xampp\htdocs\swimwear2\resources\views\category\index.blade.php)
  • @GauravRai 未定义变量:f3407ad99fc10e7e2c6983562937345f02f5bf88.php(第 84 行)中的产品(查看:C:\xampp\htdocs\swimwear2\resources\views\category\index.blade.php)
  • 发给你完整的controller方法,用resources\views\category\index.bla‌​de.php

标签: php laravel laravel-5.4


【解决方案1】:

你基本上是在做

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()-&gt;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);
    }  

【讨论】:

    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 2012-02-09
    • 2023-03-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多