Select a table based on the selection result from another table
I have a table Registrationrequests
where I have course_id
both user_id
and some other field.
$users_id = Registrationrequest::where('course_id', $query_course_user)->where('registered', 1)->get();
From the above, query
it gives me an array of results. But I need to drill down to the user_id data from another table Users
. I am using Laravel. Table models: Registrationrequest
andUser
How can I get user data from the above result? I'm not that good at Joins. Any advice?
source to share
Use Eloquent method whereHas
:
$courseId = Request::get('course_id');
$users = User::whereHas('registrationRequests', function($query) use ($courseId)
{
$query->where('course_id', $courseId)->where('registered', 1);
});
It assumes that you have created the correct relationship in your model User
. If not, add this method to your user model:
public function registrationRequests()
{
return $this->hasMany('Registrationrequest');
}
source to share