Posts Learn Components Snippets Categories Tags Tools new About
/

Laravel Nova Scout Integration with MeiliSearch

Learn how to integrate Laravel Nova Scout with MeiliSearch to give your Nova Dashboard powerful search engine

Created on Aug 16, 2021

135 views

By default, Laravel Nova searches any available resource with the defined database columns.  Having this as the default option is good enough but for a smarter and typo tolerant search, you will have to integrate Laravel Scout to Laravel Nova.

How to Enable Laravel Scout for Laravel Nova?


To enable the scout driver in Laravel nova with the MeiliSearch driver, you can use the "Searchable" trait available from "Laravel\Scout\Searchable". By using this trait, the associated Nova resource will automatically use Scout when performing searches against the resources.
Imagine you are having a Post model and you want it to be searchable, you can define it as follows.
<?php

namespace App\Models;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Post extends Model
{
    use HasFactory, Searchable;

    // your other codes here
}

How to Customize Scout Searches for Laravel Nova?


To customize the scout search result, you can override the "scoutQuery()" method on the resource you want to customize.
use Laravel\Nova\Http\Requests\NovaRequest;

/**
 * Build a Scout search query for the given resource.
 *
 * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
 * @param  \Laravel\Scout\Builder  $query
 * @return \Laravel\Scout\Builder
 */
public static function scoutQuery(NovaRequest $request, $query)
{
    return $query;
}

How to Limit Scout Result for Laravel Nova?


If you would like to limit the number of search results then you can set the "scoutSearchResults" property.
public static $scoutSearchResults = 200;

How to Disable Laravel Scout for Specific Resource in Laravel Nova?


Otherwise, if you prefer Laravel Scout to be disabled for some of your models, then you can return false to the "usesScout()" method.
/**
 * Determine if this resource uses Laravel Scout.
 *
 * @return bool
 */
public static function usesScout()
{
    return false;
}

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)