Try
$site = Site::where('businessunit',$queryParams['filterByBusinessUnitShortName'])->with('courses')->first();
return $site->courses;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi there,
i am having a small issue with a where query on a relation. When ever i try to get $courses, it returns an empty array - however, if i dump the same query i'll receive the desired results.
dd($query->where('businessunit', $queryParams['filterByBusinessUnitShortName'])->get());
Here is the controller where the query is being used.
//CourseController.php
$queryParams = $request->query();
$courses = Course::whereHas('siteObject', function ($query) use ($queryParams) {
$query->where('businessunit',$queryParams['filterByBusinessUnitShortName']);
})->get();
return $courses;
Relation on the course class
//Course class
public function siteObject() {
return $this->belongsTo(Site::class, 'site');
}
Could anyone point me the issue here?
Please or to participate in this conversation.