[英]Select a table based on select result from another table
I have a table Registrationrequests
where I have course_id
and user_id
and some other field. 我有一个表Registrationrequests
我有course_id
和user_id
以及其他一些字段。
$users_id = Registrationrequest::where('course_id', $query_course_user)->where('registered', 1)->get();
From the above query
it gives me an array of result. 从上面的query
它给出了一个结果数组。 But I need to take the details of these user_id from another table Users
. 但我需要从另一个表Users
中获取这些user_id的详细信息。 I'm using Laravel. 我正在使用Laravel。 Table models are Registrationrequest
and User
表模型是Registrationrequest
和User
How can I get the user details from the above select result? 如何从上面的选择结果中获取用户详细信息? I'm not that good in Joins. 在加入我不是那么好。 Any advice? 有什么建议?
Use Eloquent's whereHas
method: 使用whereHas
的whereHas
方法:
$courseId = Request::get('course_id');
$users = User::whereHas('registrationRequests', function($query) use ($courseId)
{
$query->where('course_id', $courseId)->where('registered', 1);
});
This assumes you have set up the proper relationship in your User
model. 这假设您已在User
模型中设置了正确的关系。 If not, add this method to your user model: 如果没有,请将此方法添加到您的用户模型:
public function registrationRequests()
{
return $this->hasMany('Registrationrequest');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.