[英]laravel, query builder, (or)where query not working as expected
this my laravel query 这是我的laravel查询
$Product = Product::select('id','ProductWorkpiece','ProductCategory','ProductName','ProductImage','ProductSubCategory')
->where('ProductCategory',$category)
->where(function ($query) use ($ProductWorkpiece,$ProductMaterial,$ProductSubCategory,$ProductBrand) {
foreach($ProductBrand as $key => $ProductBrandd) {
if (!empty($ProductBrandd) && empty($subcategory)) {
$query->orWhere('ProductBrand', '=', $ProductBrandd);
}
foreach($ProductSubCategory as $key => $subcategory) {
if (!empty($ProductBrandd) && !empty($subcategory)) {
$query->where('ProductSubCategory', '=', $subcategory);
}
}
}
})->where('Status','=','1')->get();
actually works but there is a strange one, when i click 1 check box works but when i click to 2 why not work by example I have a brand with the name sisma and have 2 product subcategory with the name Laser Marking Machine brand sisma when I checked brand sisma, out 2 product and when I checked the subcategory Dot Marking Machine and both items are missing. 实际上有效,但是有一个奇怪的问题,当我单击1时复选框有效,但是当我单击2时为什么不起作用,例如,当我单击时,我有一个名称为sisma的品牌,并且有2个产品类别为激光打标机品牌sisma检查了品牌sisma,淘汰了2种产品,当我检查了子类别打点机时,两个项目都丢失了。 and that's right and when I checked the subcategory Laser Marking Machine and 2 the product did not come out and when I unchecked the Dot Marking Machine stuff came out and I call it strange that it should be without unchecked can be seen 2 product 没错,当我检查了激光打标机子类别2时,产品没有出来;当我未选中点打标机的东西出来时,我觉得很奇怪,应该看到未经检查的产品2
if you still dont understand, watch this video is only 12 seconds https://youtu.be/fY417NsZmHI 如果您仍然不明白,请观看此视频仅12秒https://youtu.be/fY417NsZmHI
and this is my manual sql ,it works well 这是我的手动sql,效果很好
SELECT * FROM Product WHERE ProductCategory = 'Marking' AND ProductBrand = 'Sisma' and ProductSubCategory = 'Dot Marking Machine'
or ProductSubCategory = 'Laser Marking Machine'
the contents and type of $ProductSubCategory $ ProductSubCategory的内容和类型
Array
(
[0] => Dot Marking Machine
[1] => Laser Marking Machine
)
the contents and type of $ProductBrand $ ProductBrand的内容和类型
Array
(
[0] => Sisma
)
Rewriting the query like this helped getting the result combinations needed 这样重写查询有助于获得所需的结果组合
$Product = Product::select('id','ProductWorkpiece','ProductCategory','ProductName','ProductImage','ProductSubCategory')
->where('ProductCategory',$category)
->where('Status', '1')
->whereIn('ProductBrand', $ProductBrand)
->where(function ($query) use ($ProductWorkpiece,$ProductMaterial,$ProductSubCategory,$ProductBrand) {
foreach($ProductSubCategory as $key => $subcategory) {
if (!empty($subcategory)) {
$query->orwhere('ProductSubCategory', '=', $subcategory);
}
} })->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.