@manjumjn try to change:
$query->where(function ($query) use ($subject_array) {
$query->whereIn('subjects_ids', $subject_array);
});
to:
$query->whereIn('subjects_ids', $subject_array);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have defined scopeFilter in Model which has following code
public function scopeFilter($query, array $filters)
{
$query->when($filters['subjects'] ?? null, function ($query, $data) {
//Subjects ids will be comma seperated string eg : 127,125
$subject_array = explode(',', htmlspecialchars_decode($data));
$query->where(function ($query) use ($subject_array) {
$query->whereIn('subjects_ids', $subject_array);
});
})->when($filters['topics'] ?? null, function ($query, $data) {
//topics ids will be comma seperated string eg : 127,125
$topic_array = explode(',', htmlspecialchars_decode($data));
$query->where(function ($query) use ($topic_array) {
$query->whereIn('topics_ids', $topic_array);
});
})->when($filters['search'] ?? null, function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
});
});
}
Here is how I'm using this in controller
$exams = Exams::filter(\Request::only('search', 'subjects','topics'))
->orderBy('status', 'DESC')
->orderBy('updated_at', 'DESC')->transform(....);
If this is passed in URL query then the result will be empty. Am I doing anything wrong here?
Basically the are three filters, the search works fine but subjects and topics won't work.
Please or to participate in this conversation.