vendredi 6 septembre 2019

Why Laravel does not make lots of queries when using exists on relations?

I have such entities as:

  • Company
  • Person

Company hasMany Persons. So in the persons table there is company_id column.

I return company list, which I pass to CompanyResource. There I return has_persons => $this->persons()->exists() value.

Then I checked the result of DB::getQueryLog() and I found out that there is only one SQL query, which does not have count or anything like that.

In order to count how many persons a company has, Laravel should make one query per company, shouldn't it? Like select count (*) from persons where company_id = 5 for example



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire