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

dcranmer's avatar

Eager Loading BelongsTo Relation Not Working (solved)

Tables:

  • projects (id, title, school_id)
  • schools (id, school_name)
  • counties_projects (pivot)
  • counties (id, county_name)
  • sites (id, project_id, site_name)

Relations:

Project model

public function school()
  {
    return $this->belongsTo(School::class);
  }

  public function counties()
  {
    return $this->belongsToMany(County::class, 'counties_projects');
  }

  public function sites()
  {
    return $this->hasMany(Site::class);
  }

School model

public function projects()
  {
    return $this->hasMany(Project::class);
  }

County model

public function projects()
  {
    return $this->belongsToMany(Project::class, 'counties_projects');
  }

Site model:

public function project()
  {
      return $this->belongsTo(Project::class);
  }

If I run the following query from the CountiesController, where $county->id is injected via route model binding:

return $county->projects()->with(['school', 'sites'])->get();

Laravel gets both projects and sites just fine, but not school.

The first query looks like this:

select `id`, `project_name`, `description`, `tangible_outcomes`, `department`, `counties_projects`.`county_id` as `pivot_county_id`, `counties_projects`.`project_id` as `pivot_project_id` from `projects` inner join `counties_projects` on `projects`.`id` = `counties_projects`.`project_id` where `counties_projects`.`county_id` = 40 and `status_id` = 3

The query for sites looks like this:

select * from `sites` where `sites`.`project_id` in (5, 8, 11, 23, 24, 25, 30, 39, 44, 45, 49, 55, 69, 73, 82, 88, 94)

The query for eager loading the schools looks like this:

select * from `schools` where 0 = 1

"where 0 = 1"? What's going on with this query? Obviously that's not right.

In sum, eager loading is working with one relation (sites), but not the other (school).

Any ideas or suggestions?

0 likes
8 replies
MichalOravec's avatar

Why do you have there status_id = 3?

It can't be from this query

return $county->projects()->with(['school', 'sites'])->get();
dcranmer's avatar

Yes, sorry. I tried trimming the original query down to simplify it, but pasted in a longer version by mistake. It doesn't (or shouldn't) affect the eager loading.

The query should look like this:

select `id`, `project_name`, `description`, `tangible_outcomes`, `department`, `counties_projects`.`county_id` as `pivot_county_id`, `counties_projects`.`project_id` as `pivot_project_id` from `projects` inner join `counties_projects` on `projects`.`id` = `counties_projects`.`project_id` where `counties_projects`.`county_id` = 40
MichalOravec's avatar
Level 75

As you can see you don't have school_id column in that query.

Show your Project model.

dcranmer's avatar

Ok, you can give me the "idiot of the day" award. It's amazing how you can stare at something for so long and overlook what someone else can see in thirty seconds. ;-)

Thank you!

dcranmer's avatar

Negligence. Because I forgot to include it.!

MostafaGamal's avatar

A temporary and quick solution for this

In your Project.php model

protected $with = ['school']; 

You now don't need to load it in your controller it will be loaded with each project instance by default

Please or to participate in this conversation.