我有一个在user_id列中使用where Auth::id()查询已验证用户的所有结果的forms_table。
这些行中的每一行都有一个boolean列alternation。如果此值为1,则表示在另一个名为form_alternation的表中有一行,其中包含Auth::id()。这意味着在初始forms_table中不存在该Auth::id()。
如何使用where子句where('user_id', Auth::id())从forms_table中检索行,并在form_alternations中显示Auth::id()所在的行
目前我有这个,它显示了0行,因为最后的Auth::id()子句。
$baseQuery->with(['alternated' => function($q){
$q->where('user_id', Auth::id());
}]);
$baseQuery->where('user_id', Auth::id());发布于 2020-04-27 17:12:43
在我看来,您希望您的查询执行类似的操作(使用半个伪代码)。
where (forms.user.id = Auth::id() OR (users.alternation = 1 AND alternated.user_id = Auth::id()))这可以用Eloquent来实现,因为我理解你的领域,尝试这样的东西。
use Illuminate\Database\Eloquent\Builder;
// wrap it in where, to avoid other queries precedence ruining the or clause
$query->where(function (Builder $query) {
// First part, where we assume user id is in forms
$query->where('user_id', Auth::id());
// Second part, check alternation or your relationship
$query->orWhere(function (Builder $query) {
$query->where('alternation', true);
// Use whereHas to check if your forms has the user as a relationship
$query->whereHas('alternated', function (Builder $query) {
$query->where('user_id', Auth::id());
});
});
});这应该会提供与伪例类似的逻辑,只需使用orWhere()逻辑,您就可以使用whereHas()检查关系。
https://stackoverflow.com/questions/61455158
复制相似问题