Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adamjhn's avatar

How to achieve this complex query? (get the email of all users that did a registration in a specific conference and this registration has 1 or more participants associated with a specific registration type)

I have this query below to get the email of all users that have a registration in a specific conference.

$users = User::whereHas('registrations', function ($query) use($conferenceID) {
    $query->where('conference_id', '=', $$conferenceID);
})->get();

And dd($users) shows array:2 [▼ 0 => "....mail.com" 1 => "....mail.com"]. So its working fine.

But I want to get only the emails of the users that did a registration in a specific conference ($conferenceID) and this registration has 1 or more participants associated with a specific registration type.

For example, if the user selects the registration type "General" in a select menu and click in "Get Emails" I want to pass the registration type value selected by the user (ex: RegistrationType01) to the query and get the email of all users that did a registration in the conference with id "1" but only get the email of all users that did a registration in that conference with "id" 1 and that registration has 1 or more participants with the registration type "RegistrationType01").

Do you know how to properly achieve this query? I have the code below but it shows "Call to undefined relationship [Registration] on model [App\Conference]".

$getEmailsOfAspecificRegistrationType = Conference::with('registrations', 'Registration.registrationTypes', 'Registration.registrationTypes.participants')->where('id', $id)->get();

0 likes
11 replies
adamjhn's avatar

Relevant models for the question:

Users model:

public function registrations(){
    return $this->hasMany('App\Registration','user_that_did_registration');
}

Registration model:

// user that did the registration
public function customer(){
    return $this->belongsTo(User::class, 'user_that_did_registration', 'id');
}
 public function participants(){
    return $this->hasMany('App\Participant');
}

public function conferenec(){
    return $this->belongsTo('App\Conference');
}

Conference Model:


 public function registrations(){
    return $this->hasMany('App\Registration', 'conference_id');
}

Participants mode:

public function registration(){
    return $this->belongsTo('App\Registration');
}

public function registration_type(){
    return $this->belongsTo('App\RegistrationType');
}
_Artak_'s avatar

use again whereHas


$users = User::whereHas('registrations', function ($query) use($conferenceID) {
    // $query - registrations , registrations whereHas participants
    $query->where('conference_id', '=', $conferenceID)->whereHas('participants');
})->get();


If you just use whereHas('relation') that means you only want to get the models that have at least one related model in this relation. whereHas() allows you to specify additional filters for the related model to check.

1 like
adamjhn's avatar

Thanks but so maybe is necessary one more whereHas? Because the participants table thas the column "registration_type_id" and I want to get only the emails of the users that did a registration in a specific conference and that registration has 1 or more participants associated with a specific registration type (ex: RType01).

I dont want to get the email of all users that did a registration in a conference i just want to get the email of the users that did the registration in a conference and this registration has 1 or more participantas associated with a specifi registration type. The info about the participants and the registration types associated with the registration are in the participants table like:


id     registration_id (fk)     registration_type_id(fk)     name        
1         1                        1                              Jake 
2        1                        1                              Jane
3        1                       2                             Paul
4        2                      1                              Ben         

And the registrations table:

id   status                user_that_did_registration            conference_id 

1         Incomplete             1                                     1
2        Incomplete             2                                     1



_Artak_'s avatar
_Artak_
Best Answer
Level 14

using multiple "whereHas" is normal practice, but if you don't want filter you can just use "has()": "whereHas()" works basically the same as "has()" but allows you to specify additional filters for the related model to check.



$users = User::whereHas('registrations', function ($query) use($conferenceID) {
    // $query - registrations , registrations whereHas at least 1 participant
    $query->where('conference_id', '=', $conferenceID)->whereHas('participants',    function($q){
    $q->where('registration_type_id', '=', 1);
});
})->get();

1 like
adamjhn's avatar

Thanks, do you know how to instead of have the static value "1" have the registration id? Like below dont works it appears undefined variable use($request):

else{
            $sendTo = User::whereHas('registrations', function ($query) use($id) {

                $query->where('conference_id', '=', $id)->whereHas('participants',    function($q) use($request){
                    $q->where('registration_type_id', '=', $request->rtype);
                });
            })->get();
        }
_Artak_'s avatar

you must pass use($request->rtype) two time.


else{
            $sendTo = User::whereHas('registrations', function ($query) use($id, $request->rtype) {
            // and use again $request->rtype
                $query->where('conference_id', '=', $id)->whereHas('participants',    function($q) use($rtype, $request->rtype){
                    $q->where('registration_type_id', '=', $request->rtype);
                });
            })->get();
        }


1 like
adamjhn's avatar

Thanks, but adding $request->rtype to the first use() like: " $sendTo = User::whereHas('registrations', function ($query) use($id, $request->rtype) { " shows "Expected: ).

_Artak_'s avatar

make sure that there is no mistake, I use like this and it work fine. and what exactly error message are you receiving

adamjhn's avatar

With:

$sendTo = User::whereHas('registrations', function ($query) use($id, $request->rtype) {
                $query->where('conference_id', '=', $id)->whereHas('participants',    function($q) use($request->rtype){
                    $q->where('registration_type_id', '=', $request->rtype);
                });
            })->get();

Shows:


Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ',' or ')'


_Artak_'s avatar

instead of

// fisrt time
use($id, $request->rtype)
// second time
use($request->rtype)


change to

// first time
use($id, $request)
//  second time
use($request)

then


$q->where('registration_type_id', '=', $request->rtype);

1 like
Cronix's avatar

or just use the request() helper. You don't have to pass that in, it's available globally.

1 like

Please or to participate in this conversation.