Php 如果else语句不起作用,则使用laravel筛选器
我试图过滤我的产品的价格性别颜色 这是我使用类似url localhost/category/tag?price=100时的工作代码Php 如果else语句不起作用,则使用laravel筛选器,php,laravel,laravel-5.4,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')) { $product
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);
}
我尝试添加其他类似的条件
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);
}
if(request()->has('gender')){
$products=Product::withAllTags($tags)->where('gender',request('gender'))->get();
}
如果(request()->has('price')){
$products=Product::withAllTags($tags)->where('price'),'
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);
}
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();
}
如果(request()->has('price')){
$products=Product::withAllTags($tags)->where('price'),'
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);
}
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();
}
如果(request()->has('price')){
$products=Product::withAllTags($tags)->何处('price',未定义变量在哪一行和哪一页?未定义变量:products(视图:C:\xampp\htdocs\swimwear2\resources\views\category\index.blade.php)在f3407ad99fc10e7e2c6983562937345f02f5bf88.php(第84行)@GauravRai未定义变量:products(视图:C:\xampp\htdocs\swimwear2\resources\views\category\index.blade.php)在f3407ad99fc10e7e2c6983562937345f02f5bf88.php(第84行)中,使用resources\views\category\index.blaa发布完整的controller
方法de.php
f3407ad99fc10e7e2c6983937345f02f5bf88.php(第84行)中的未定义变量:产品(视图:C:\xampp\htdocs\swimwear2\resources\views\category\index.blade.php)在f3407ad99fc10e7e2c6983562937345f02f5bf88.php(第84行)中,使用resources\views\category\index.bla发布完整的controller
方法de.php