Integrations / Frameworks / Laravel / Server-Side Search

Server-Side Search

Searching

You can search in a Searchable class using the search method. The search method accepts a single string to search in your Searchable class index:

1
$articles = Article::search('Star Trek')->get();

You can also search with pagination, and work with soft deleted models. Head over to the Laravel Scout documentation to find out more.

Search with numerical filters

You can use the where method to filter your search results. With Scout Extended, this method shares the same API as the Laravel Query Builder, allowing you to filter results either via a comparison or a numerical range:

1
2
3
$articles = Article::search('Star Trek')->where('views', '>', 100)->get();
$articles = Article::search('Star Trek')->where('created_at', '>=', now()->subDays(7))->get();
$articles = Article::search('Star Trek')->where('views', 100)->get(); // views = 100

The where method supports the following operators: <, <=, =, !=, >=, >.

Search with numerical range filters

The whereBetween method filters results for which the provided field falls between the given range:

1
2
3
$products = Products::search('Star Trek')
    ->whereBetween('price', [100, 200])
    ->get();

Search with multiple filter values

The whereIn method filters results for which the value of the provided field is part of the given array:

1
2
3
$products = Products::search('Star Trek')
    ->whereIn('id', [1, 2])
    ->get();

Search with locational filters

The aroundLatLng method adds a geolocation parameter to the search request. You can define a point with its coordinates. This method is syntactic sugar, and you can use the method with to specify more location details such as aroundLatLng and aroundRadius.

1
2
3
$articles = Article::search('query')
    ->aroundLatLng(48.8588536, 2.3125377)
    ->get();

Search with custom search parameters

The with method gives you complete access to customize search API parameters.

1
2
3
4
5
6
$articles = Article::search('Star Trek')
    ->with([
        'hitsPerPage' => 30,
        'filters' => 'attribute:value',
        'typoTolerance' => false,
    ])->get();

Retrieve the number of hits

The count method returns the number of hits the query matches:

1
$count = Article::search('Star Trek')->count();

Retrieve hit metadata

You can use the scoutMetadata method to retrieve an array with more information about any hit.

The key _highlightResult holds all the highlighted attributes. By default, Algolia highlights all the searchable attributes. You can change this with the aroundLatLng parameter.

1
2
$metadata = Article::search('Star Trek')->get()->first()->scoutMetadata();
$highlightResult = $metadata['_highlightResult'];

If you’ve set the getRankingInfo search parameter to true, the _rankingInfo holds detailed ranking information. It lets you see which ranking criteria played a role in selecting each model:

1
2
$metadata = Article::search('Star Trek')->get()->first()->scoutMetadata();
$rankingInfo = $metadata['_rankingInfo'];

Did you find this page helpful?