You can try to add Courses::whereHas('centermapping.centerdail') to your query.
Tell me if it helps ;).
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have tables like below,
course_center_mapping
id, course_id,center_id,mode_availability
1, 1, 1, Both
1, 1, 2, Offline
1, 1, 3, Online
Course.php (Model)
public function centermapping()
{
return $this->hasMany('App\CourseCenterMap','course_id','course_id');
}
CourseCenterMap.php
public function centerdetail()
{
return $this->belongsTo('App\Centers','center_id','id');
}
In controller i have a code like below,
$modes = ['Both','Offline'];
$coursedetails = Courses::with(['centermapping'=> function($q) use($modes) {
$q->where('status','Active')
->whereIn('mode_availability',$modes);
},'centermapping.centerdetail' => function($q) {
$q->where('active',1);
}])->where('menu_name',$courseslug)->where('course_active',1)->first();
and above query is working fine but i want to filter out centermapping if centerdetail is null.
I assume you mean a relation must exist? Or is it a column on centerdetail that cannot be null?
$coursedetails = Courses::with(['centermapping'=> function($q) use($modes) {
$q->where('status','Active')
->whereIn('mode_availability',$modes)
->has('centerdetail');
},'centermapping.centerdetail' => function($q) {
$q->where('active',1);
}])->where('menu_name',$courseslug)->where('course_active',1)->first();
Please or to participate in this conversation.