Laravel Eloquent 其中 0 = 1
Laravel Eloquent WhereIn 0 = 1
我尝试使用 whereIn
子句,但出现了罕见的行为。
这是我的关系:
return $this->belongsToMany('App\Models\Role', 'system_users_roles', 'user_id', 'role_id')
->where(function ($query) {
$query->where(function ($querypermissions) {
$querypermissions->whereNotNull('service_id')
->whereHas('permissions', function ($q) {
$q->whereIn('app_id', $this->apps->pluck('id'));
});
})
->orWhereNULL('service_id');
});
此代码 return 一些行,但缺少一行(在本例中是我需要的行)。
如果我改变这一行:
$q->whereIn('app_id', $this->apps->pluck('id'));
给这个:
$q->whereIn('app_id', [0 => 1]);
我得到了正确的结果。我得到了我需要的行的第一个结果。
我以为是$this->apps->pluck('id')
的问题,因为它可能是空的,但它不是
我试过 $this->apps
、$this->apps->pluck('id')->toArray()
,但没有任何效果。
谁能告诉我哪里出了问题!?
更多信息:当我显示 sql 查询(使用 ->toSql()
)时,它有:
bla bla bla...
and 0 = 1))
bla bla bla...
就像说数组不正确.
但是,正如我之前所说,我尝试了不同的方法来创建一个值为 whereIn 的数组,但没有任何效果。
我读了这个 但我需要额外的帮助。
谢谢!
我发现了问题。
$this->apps->pluck('id')
是同型号的关系。并且任何 'role' 都有不同的值。
我更改了代码,添加了一个包含我需要的所有应用程序的变量:
$apps = Auth::user()->apps->pluck('id');
现在工作正常。
我尝试使用 whereIn
子句,但出现了罕见的行为。
这是我的关系:
return $this->belongsToMany('App\Models\Role', 'system_users_roles', 'user_id', 'role_id')
->where(function ($query) {
$query->where(function ($querypermissions) {
$querypermissions->whereNotNull('service_id')
->whereHas('permissions', function ($q) {
$q->whereIn('app_id', $this->apps->pluck('id'));
});
})
->orWhereNULL('service_id');
});
此代码 return 一些行,但缺少一行(在本例中是我需要的行)。
如果我改变这一行:
$q->whereIn('app_id', $this->apps->pluck('id'));
给这个:
$q->whereIn('app_id', [0 => 1]);
我得到了正确的结果。我得到了我需要的行的第一个结果。
我以为是$this->apps->pluck('id')
的问题,因为它可能是空的,但它不是
我试过 $this->apps
、$this->apps->pluck('id')->toArray()
,但没有任何效果。
谁能告诉我哪里出了问题!?
更多信息:当我显示 sql 查询(使用 ->toSql()
)时,它有:
bla bla bla...
and 0 = 1))
bla bla bla...
就像说数组不正确.
但是,正如我之前所说,我尝试了不同的方法来创建一个值为 whereIn 的数组,但没有任何效果。
我读了这个
谢谢!
我发现了问题。
$this->apps->pluck('id')
是同型号的关系。并且任何 'role' 都有不同的值。
我更改了代码,添加了一个包含我需要的所有应用程序的变量:
$apps = Auth::user()->apps->pluck('id');
现在工作正常。