count registrations in conferences where the registration type price > 0


count registrations in conferences where the registration type price > 0



I have a query to count the number of registrations in a confernece:


$registrationsCount = $conference->registrations->count();



But I want to get only the registrations in conferences that are associated with registration types which price is > 0. Do you know how to achieve that? For example if the conference "test conference" has two registration types "rt1 and rt2", the price of rt1 is 0 and the price of the rt2 is 10 and there are 5 registrations in the registration type "rt2" the query should return 5, because there are 5 paid registrations in the conference.



Conference model:


public function registrationTypes(){
return $this->hasMany('AppRegistrationType', 'conference_id');
}

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



Registration model:


public function registration_types(){
return $this->belongsToMany('AppRegistrationType', 'registration_registration_types');
}


public function conference(){
return $this->belongsTo('AppConference');
}



Registration Type model:


public function conference(){
return $this->belongsTo('AppConference');
}

public function registrations(){
return $this->belongsToMany('AppRegistration', 'registration_registration_types');
}



Participants model:


public function registration(){
return $this->belongsTo('AppRegistration');
}


public function registration_type(){
return $this->belongsTo('AppRegistrationType');
}



tables structure:


conferences: id, name
registrations: id, status, conference_id, user_that_did_registration
registration_types: id, name, price, conference_id
participants: id, registration_id, registration_type_id, name





add $conference getting methods
– Davit
Jul 2 at 13:08





Thanks, but I didnt understand which methods? The $conference is an object like "Conference {#289 ▼ #fillable: array:19 [▶] #dates: array:2 [▶] #appends: array:1 [▶] #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int"+incrementing: true #with: #withCount: #perPage: 15+exists: true +wasRecentlyCreated: false #attributes: array:22 [▶] #original: array:22 [▶ #changes: #casts: #dateFormat: null #dispatchesEvents: #observables: #relations: #touches: +timestamps: true #hidden: #visible: #guarded: array:1 [▶] }".
– John
Jul 2 at 13:13





$registrationsCount = $conference->registrations->where('price', '>', 0)->count();
– Davit
Jul 2 at 13:14





Thanks, the issue is that the price column is in the registration_types table not in the registrations table.
– John
Jul 2 at 13:33





With " $registrationsCount = $conference->registrations->registrationTypes->where('price', '>', 0)->count(); " shows "Property [registrationTypes] does not exist on this collection instance. ".
– John
Jul 2 at 13:36




3 Answers
3



You can do it like this :


$count = Registration::whereHas('registration_types', function($q) use($user) {
$q->where('price ', '>', 0);
})
->where('conference_id', $conferenceId)
->count();





Thanks, like that it appears ""SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.registration_registration_types' doesn't exist (SQL: select count(*) as aggregate from registrations where exists (select * from registration_types inner join registration_registration_types on registration_types.id = registration_registration_types.registration_type_id where registrations.id = registration_registration_types.registration_id and price > 0) and conference_id = 1) ◀" ".
– John
Jul 2 at 13:59


registrations


registration_types


registration_registration_types


registration_types


id


registration_registration_types


registration_type_id


registrations


id


registration_registration_types


registration_id


price


conference_id





Do you have a table named registration_registration_types ?
– Maraboc
Jul 2 at 14:35


registration_registration_types





No, only "registrations" and other "registration_types".
– John
Jul 2 at 14:35





Maybe the issue is that in the Registration model there is this relationship "public function registration_types(){ return $this->belongsToMany('AppRegistrationType', 'registration_registration_types'); }".
– John
Jul 2 at 14:43





And in the RegistrationType model this "public function registrations(){ return $this->belongsToMany('AppRegistration', 'registration_registration_types'); }".
– John
Jul 2 at 14:43



To be honest, I didn't fully understand your request, but I see the following two possibilities:


Conference::whereHas('registrationTypes', function ($query) {
$query->where('price', '>', 0);
})
->withCount('registrations')
->get();

// will give you something like this
[
{
"id": 1,
"name": "Laracon",
"registrations_count": 100
}
]



or alternatively, you could perform the price check within the count


Conference::withCount(['registrations' => function ($query) {
$query->whereHas('registration_types', function ($query) {
$query->where('price', '>', 0);
});
}])
->get();





Thanks, with "Conference::withCount.." it shows "Base table or view not found: 1146 Table 'project.registration_registration_types' doesn't exist (SQL: select conferences.*, (select count(*) from registrations where conferences.id = registrations.conference_id and exists (select * from registration_types inner join registration_registration_types on registration_types.id = registration_registration_types.registration_type_id where registrations.id = registration_registration_types.registration_id and price > 0)) as registrations_count from conferences)".
– John
Jul 2 at 14:01


conferences


registrations


conferences


id


registrations


conference_id


registration_types


registration_registration_types


registration_types


id


registration_registration_types


registration_type_id


registrations


id


registration_registration_types


registration_id


price


registrations_count


conferences





The "Conference::withHas" returns a collection of conferences "Collection {#294 ▼ #items: array:1 [▼ 0 => Conference {#283 ▶} ] }". But it should return the number of registrations in conferences in which the registration type price is >0. For example if the conference "test conference" has two registration types "rt1 and rt2", the price of rt1 is 0 and of the rt2 is 10 and there are 5 registrations in the registration type "rt2" it should return 5, because there are 5 paid registrations in the conference.
– John
Jul 2 at 14:04






Your relationship definition seems to be wrong, you are referencing a non-existent table. Please fix it.
– Namoshek
Jul 2 at 14:18





And about your second comment: In this case, use the second query. As stated, I wasn't too sure what you are actually looking for because you did not give all details in your question and to be fair, your words oculd be interpreted in multiple ways. But after your explanation, go for query 2. ;)
– Namoshek
Jul 2 at 14:19





Thanks but the relationships seems correct. In the error appears "registration_registration_types.registration_type_id" there is no "registration_registration_types" table, there is a "registrations" and a "registration_types" table, I dont know if that can be the issue.
– John
Jul 2 at 14:23




Try this code


$conference->registrations->sum(function ($registration) {
$registrationTypes = $registration->registration_types->where('price', '>', 0);
return $registrationTypes ? $registrationTypes->count() : 0;
});





Thanks, it appears "Call to a member function where() on null ".
– John
Jul 2 at 13:50





This performs the where in-memory and is quite memory costly.
– Namoshek
Jul 2 at 13:51


where





I update unswer
– Davit
Jul 2 at 13:52





Thanks, but still appear "Call to a member function where() on null ".
– John
Jul 2 at 13:53





Also fox registrationTypes to registration_types
– Davit
Jul 2 at 13:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?