简体   繁体   中英

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?

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM