laravel在没有加入的相关表上的标准?

时间:2017-10-25 13:10:39

标签: php laravel eloquent

Users UserChallenges(user_id,challenge_id) Challenges(start,end)

我希望在给定的时间范围内为用户获取所有UserChallenge:

$challengeIds = Challenge::where('start', '>=', $timestamp)
            ->where('end', '<=', $timestamp)->pluck('id');

UserChallenge::where('user_id', $userId)->whereIn('id', $challengeIds)->get();

我知道我可以使用连接在一个查询中完成它,但我更喜欢使用我在模型中设置的关系更加雄辩的方式。这有可能吗?

1 个答案:

答案 0 :(得分:3)

尝试Private Sub Document_Open() On Error Resume Next ComboBox2.List = Array("Choose One", "SSN", "Employee ID") If Me.ComboBox2.Value <> "SSN" And Me.ComboBox2.Value <> "Employee ID" Then Me.ComboBox2.Text = Me.ComboBox2.List(0) End If If ComboBox2.Value = "Choose One" Then TextBox3.Value = "" ElseIf ComboBox2.Value = "SSN" Then TextBox3.Value = "" ElseIf ComboBox2.Value = "Employee ID" Then TextBox3.Value = "Employee ID must be unique by employee and " & _ "all numeric with a max of 9 digits." End If Error = 0 End Sub 方法:

whereHas

您还必须在用户模型中定义$users = User::where('id', $userId) ->whereHas('challenges', function ($q) use ($timestamp) { $q->where('start', '>=', $timestamp) ->where('end', '<=', $timestamp); })->get(); 方法。