TitasGailius / nova-search-relations

This package allow you to include relationship columns into Laravel Nova search query.
Other
353 stars 33 forks source link

Support splitting search term #41

Closed rderks88 closed 9 months ago

rderks88 commented 1 year ago

Hi Titas Gailius,

First of all, great package and thank you for your work!

I've been using it for quite some time, and had a little idea for an update which came from my own experience. If we give the oppurtunity to define a character to split the search term by, you can very easily get nice sets of search results in your index view. I've had that built into my own Nova setup for a long time, and just integrated that into your package. By default it could be turned of, but for people who like it, we can offer the oppurtunity by just overriding the "splitCharacter".

Eager to hear what you think. Rob

rderks88 commented 1 year ago

I just noticed as I created this PR that within Nova 4.0 I'm using your package redundantly. Does not take away from the fact that for previous versions it might be relevant, so I'm just leaving the PR for you to decide.

Within Nova 4, you can just implement this with using the following method within your baseResource (if you have one)

protected static function applySearch($query, $search)
    {
        $query->where(function (Builder $query) use ($search) {
            foreach (explode(self::$searchSplitCharacter, $search) as $search) {
                $query->orWhere(function ($query) use ($search) {
                    parent::applySearch($query, $search);
                });
            }
        });
        return $query;
    }
TitasGailius commented 9 months ago

Thank you