Where 和 WhereIn 以及 WhereRaw 不适用于 mongodb 流明中的布尔值
Where and WhereIn and WhereRaw not working for boolean values in lumen with mongodb
我正在收集与公司关联的员工
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Employee extends Eloquent
{
protected $casts = [
'has_pp' => 'Boolean',
'has_eal' => 'Boolean',
'support' => 'String',
'has_cin' => 'Boolean',
'has_lac' => 'Boolean'
];
protected $fillable = [
'first_name',
'last_name',
'has_pp',
'has_eal',
'support',
'has_cin',
'has_lac'
];
public function company() {
return $this->belongsTo(Company::class);
}
}
我公司的合集是这样的
class Company extends Eloquent
{
protected $fillable = [
'name',
'total_employee',
'active',
];
public function employee() {
return $this->hasMany(Employee::class);
}
}
我想根据我正在执行以下查询的 has_pp 和 has_lac 的值过滤掉员工
$filterdata = Company::find($request->company_id);
if($request->has('pp')) {
$filterdata = $filterdata->whereHas('employee',function ($query) use($request){
$query->whereIn('has_pp',$request->pp);
});
}
$filterdata = $filterdata->get();
现在上面的查询给了我空白数组 o/p,这里 $request->pp 是数组类型
例如:- [true,false],whereIn 对所有其他值都有效,但会导致布尔类型的值出现问题。
我还尝试了一些其他查询,例如
$filterdata = $filterdata->whereHas('employee',function ($query) use($request){
$query->whereRaw(['has_pp'=>['$eq'=>false]]);
});
$filterdata = $filterdata->whereHas('employee',function ($query) use($request){
$query->where('has_pp','=',false);
});
i also had hardcoded direct values, but did'nt worked.
Pls ignore typing error
```
currently i am using
"jenssegers/mongodb": "3.5"
lumen 5.8
经过长时间的搜索,我找到了解决问题的方法。
monogdb 使用不同类型的查询来过滤布尔值 values.for 更好的理解你可以阅读这篇文章。
https://www.tutorialspoint.com/mongodb-query-for-boolean-field-as-not-true
whereRaw 在直接使用 employee collection 搜索的情况下有效,但是当我们在 whereHas 查询之后使用它时它不起作用。
所以查询对我有用
$filterdata = Employee::whereRaw(['has_pp'=>['$eq'=>false]]);
or
$filterdata = Employee::whereRaw(['has_pp'=>['$ne'=>false]]);
$eq = equalto , $ne = 不等于 mongodb.
的语法
但我仍然无法理解为什么 WhereRaw 不能与我之前的查询一起使用 WhereHas 的原因。如果你们明白原因,请分享。
我正在收集与公司关联的员工
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Employee extends Eloquent
{
protected $casts = [
'has_pp' => 'Boolean',
'has_eal' => 'Boolean',
'support' => 'String',
'has_cin' => 'Boolean',
'has_lac' => 'Boolean'
];
protected $fillable = [
'first_name',
'last_name',
'has_pp',
'has_eal',
'support',
'has_cin',
'has_lac'
];
public function company() {
return $this->belongsTo(Company::class);
}
}
我公司的合集是这样的
class Company extends Eloquent
{
protected $fillable = [
'name',
'total_employee',
'active',
];
public function employee() {
return $this->hasMany(Employee::class);
}
}
我想根据我正在执行以下查询的 has_pp 和 has_lac 的值过滤掉员工
$filterdata = Company::find($request->company_id);
if($request->has('pp')) {
$filterdata = $filterdata->whereHas('employee',function ($query) use($request){
$query->whereIn('has_pp',$request->pp);
});
}
$filterdata = $filterdata->get();
现在上面的查询给了我空白数组 o/p,这里 $request->pp 是数组类型 例如:- [true,false],whereIn 对所有其他值都有效,但会导致布尔类型的值出现问题。 我还尝试了一些其他查询,例如
$filterdata = $filterdata->whereHas('employee',function ($query) use($request){
$query->whereRaw(['has_pp'=>['$eq'=>false]]);
});
$filterdata = $filterdata->whereHas('employee',function ($query) use($request){
$query->where('has_pp','=',false);
});
i also had hardcoded direct values, but did'nt worked.
Pls ignore typing error
```
currently i am using
"jenssegers/mongodb": "3.5"
lumen 5.8
经过长时间的搜索,我找到了解决问题的方法。
monogdb 使用不同类型的查询来过滤布尔值 values.for 更好的理解你可以阅读这篇文章。 https://www.tutorialspoint.com/mongodb-query-for-boolean-field-as-not-true
whereRaw 在直接使用 employee collection 搜索的情况下有效,但是当我们在 whereHas 查询之后使用它时它不起作用。
所以查询对我有用
$filterdata = Employee::whereRaw(['has_pp'=>['$eq'=>false]]);
or
$filterdata = Employee::whereRaw(['has_pp'=>['$ne'=>false]]);
$eq = equalto , $ne = 不等于 mongodb.
的语法但我仍然无法理解为什么 WhereRaw 不能与我之前的查询一起使用 WhereHas 的原因。如果你们明白原因,请分享。