Level 75
Use orWhereHasMorph instead of whereHasMorph.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a users table with a morph relation to a profile. I'm searching for the firstname, middlename, lastname and id on the users table and contact in the student_profiles.
$query->when($filters['search'] ?? false, function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->where('firstname', 'like', '%' . $search . '%')
->orWhere('middlename', 'like', '%' . $search . '%')
->orWhere('lastname', 'like', '%' . $search . '%')
->orWhere('id', 'like', '%' . $search . '%');
});
$query->whereHasMorph('profile', StudentProfile::class, function ($query) use ($search) {
$query->where('contact', 'like', '%' . $search . '%');
});
});
My problem is when I add the whereHasMorph() or vice versa the whole query would not work. But if it's only the where() or whereHasMorph() it would work.
Use orWhereHasMorph instead of whereHasMorph.
Please or to participate in this conversation.