1

我是 laravel 的新手。如何结合这两个条件?

public function student($classroom_id)
    {
        $members = Classroom::findOrFail($classroom_id,)->members->load('role');
        $members = User::where([
            ['role_id', 2]
        ])->get();

        return response()->json($members);
    }
4

1 回答 1

0

尝试这个:

public function student($classroom_id)
{
    $members = Classroom::whereHas('members.role', function ($query) {
        $query->where('id', '2');
    })->findOrFail($classroom_id)->members->load('role');

    return response()->json($members);

}
于 2020-03-22T06:33:54.153 回答