Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

shouldBeSearchable not working? #84

Open
Jafo232 opened this issue Apr 23, 2019 · 5 comments
Open

shouldBeSearchable not working? #84

Jafo232 opened this issue Apr 23, 2019 · 5 comments

Comments

@Jafo232
Copy link

Jafo232 commented Apr 23, 2019

It seems that no matter what condition I set in shouldBeSearchable(), it doesn't matter. Example:

	public function shouldBeSearchable()
	{
		return $this->approved === 1;
	}

Even when approved is 0 for an item, it is still returned.

@iaK
Copy link

iaK commented Sep 21, 2019

Yeah, can confirm it dosen't work. Kinda hard to implement too.

You can't create the fulltext index on only certain rows, all of it has go in there.

The problem is you can't really filter them away after getting them from the db either, that causes trouble if you want to filter och limit the results :/

@nanadjei
Copy link

nanadjei commented Oct 7, 2019

Having the same issue...

@nanadjei
Copy link

nanadjei commented Oct 9, 2019

I actually fixed the issue by setting the is_published column in my database to boolean instead of a string.

@freshleafmedia
Copy link

freshleafmedia commented Oct 21, 2020

For those who need a workaround you can override the search method and manually tack on where clauses to the Builder instance:

class MyModel extends Model
{
    use Searchable {
        search as traitSearch;
    }

    public static function search($query = '', $callback = null)
    {
        // Work around for https://github.com/yabhq/laravel-scout-mysql-driver/issues/84
        return self::traitSearch($query, $callback)->where('attribute', 0);
    }
}

Be aware of the limitations: https://laravel.com/docs/master/scout#where-clauses

@rabol
Copy link

rabol commented Feb 19, 2021

@freshleafmedia the workaround works fine as long as you don't want to do do something like
whereNotNull

I got around it by doing this:

class MyModel extends Model
{
    use Searchable {
        search as traitSearch;
    }

    public static function search($query = '', $callback = null)
    {
        // Work around for https://github.com/yabhq/laravel-scout-mysql-driver/issues/84
        return self::traitSearch($query, '\App\Models\MyModel::callBack');
    }

    public static function callBack($query, $engine)
    {
        return $query->whereNotNull('field_name');
    }
}

another option is to simply add a callback when you do the ::search()

$result = MyModel::search('some text', '\App\Models\MyModel::callback')->get()

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants