Search code examples
phplaravel-7laravel-nova

Laravel Nova v3 intermittent search results at global and resource level


Nova: v3.8.2 Laravel: 7

I have a Laravel Nova app with the following App/Nova/User.php resource:

/**
 * The columns that should be searched.
 *
 * @var array
 */
public static $search = [
    'firstname', 'lastname', 'email', 'username'
];

When using the global or resource level search you can search for the username, last name and first name successfully. But search results are intermittent across all these fields. For example, whole users are missing, or any searches on emails won't work.

Though, within App/User.php we have email set to hidden:

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = [
    'email',
];

It may be to do with the later, but I'm not sure how to override this in the Nova users resource. Or it could be a separate issue altogether.


Solution

  • After some research and further testing, I found out that Nova uses Laravel Scout by default for searching. I'd assumed incorrectly the default method was a direct saearch of the database. In our case, our Scout Algolia setup has is very specific for the frontend. For example, we don't include email for security. Also, our local testing Algolia setup has a limited set of records. This was causing intermittent search results when testing.

    The best solution here is to setup Algolia correctly to support Nova. But as a quick solution we can disable Scout on a Nova resource level:

    public static function usesScout()
    {
        return false;
    }
    

    https://github.com/laravel/nova-issues/issues/1349

    Laravel Nova: Bypass the search with Scout