packy
6 months ago

Search relationship with "like"

Posted 6 months ago by packy

I am trying to search a relatiship and return a collection of that relationship by using like but not having any luck. Basically I am using Livewire for a list with a search. Once you are typing it will update the list with results based upon your search thus far. I have done this a lot when searching a model but with a relationship I am getting no results.

Example

If I do a where search with a full search parameter I know exists, it works:

public static function searchAttendees($conferenceId, $query)
    {

        $conference = static::where('id', $conferenceId)->with('attendees.socials')->first();

        return $conference->attendees->where('name', $query );

    }

but this would require a correct full $query like "John Smith", not a live search where results populate while typing. What I usually do is use like when searching but that doest seem to work with a relationship

 public static function searchAttendees($conferenceId, $query)
    {

        $conference = static::where('id', $conferenceId)->with('attendees.socials')->first();

        return $attendees = $conference->attendees->where('name', 'like', '%' . $query . '%');

    }

but this doesnt return any results when I start typing "John Smith" even though I know that record would exists on as an Attendee at that Conference

Please sign in or create an account to participate in this conversation.