user1012181
user1012181

Reputation: 8726

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.

$users_id = Registrationrequest::where('course_id', $query_course_user)->where('registered', 1)->get();

From the above query it gives me an array of result. But I need to take the details of these user_id from another table Users. I'm using Laravel. Table models are Registrationrequest and User

How can I get the user details from the above select result? I'm not that good in Joins. Any advice?

Upvotes: 3

Views: 90

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220136

Use Eloquent's whereHas method:

$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. If not, add this method to your user model:

public function registrationRequests()
{
    return $this->hasMany('Registrationrequest');
}

Upvotes: 4

Related Questions