Why do you have there status_id = 3?
It can't be from this query
return $county->projects()->with(['school', 'sites'])->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Tables:
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?
As you can see you don't have school_id column in that query.
Show your Project model.
Please or to participate in this conversation.