3

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?

1 Answer 1

4

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');
}
3
  • I tried this: $query_course_user=Request::get('course_id'); if($query_course_user!=null) { $users = User::whereHas('registrationrequests', function($query) { $query->where('course_id', $query_course_user)->where('registered', 1); }); } But shows $query_course_user as undefined variable! Dec 17, 2014 at 3:03
  • Error: Call to undefined method Illuminate\Database\Query\Builder::registrationrequests() This registrationrequests is another table Dec 17, 2014 at 3:06
  • What you mean by relationship's method name? I'm just a beginner in laravel. Sorry if I sounds dumb! Dec 17, 2014 at 3:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.