【发布时间】: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.blade.php
标签: php laravel laravel-5.4