Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stratboy's avatar

Why doesn't Scout return an instance of Meilisearch\Search\SearchResult?

I mean, am I doing something wrong?

Usually code examples are like $seeds = Model::search([...])->get(), but what I've found out is that this will return the models, with all the fields (and not only the ones I need), and also will not give me any of the additional useful data I could need, for example estimatedTotalHits, hitsCount, limit, etc.. But I really don't get why ->get() is not giving me exactly that. It's quite strange. And obviously, Scout's docs are really really incomplete.

So, am I doing something wrong? Is there some other method to retrieve the real data?

0 likes
2 replies
LaryAI's avatar
Level 58

It looks like you're trying to use Laravel Scout with Meilisearch and you want to access the raw search results from Meilisearch, including metadata like estimatedTotalHits, hitsCount, etc. However, when you use the ->get() method on the Scout builder, it returns an Eloquent collection of models, not the raw Meilisearch results.

To achieve what you want, you can directly interact with the Meilisearch client within your callback function and return the raw search results. Here's how you can modify your search method to achieve this:

In this solution:

  1. The Seed::search method is used to perform the search.
  2. The callback function directly interacts with the Meilisearch client and returns the raw search results.
  3. The raw search results are accessed using the raw() method on the Meilisearch\Search\SearchResult instance.
  4. The raw data and metadata are then passed to the view or processed as needed.

This way, you can access all the additional useful data from Meilisearch that you need.

stratboy's avatar
stratboy
OP
Best Answer
Level 5

I want to also ask by myself. First thing first, I must say that the Scout docs are really, really poor. That said, you must take a look at the code of vendor/laravel/scout/src/Builder.php. You'll find out that it has some methods that help with what I'm looking for on my question. First the raw() method, like Lary mentioned, and that's the main thing. But that said, it even has things like:

options(array $options) // here you can pass mailisearch's options
take($limit) // > the limit
raw() // meilisearch's (or other clients) real results
getTotalCount($results)
orderBy($column, $direction = 'asc')
get() // > all the models, but with all the fields, and without the client's data
cursor() // > lazyCollection
simplePaginate($perPage = null, $pageName = 'page', $page = null)
simplePaginateRaw($perPage = null, $pageName = 'page', $page = null)
paginate($perPage = null, $pageName = 'page', $page = null)
paginateRaw($perPage = null, $pageName = 'page', $page = null)

So you can use code like this:

class SearchController extends Controller{

    public function search(Request $request){
        $search_text = trim($request->input('q')) ?? '';
        $search_category = trim($request->input('categories')) ?? '';
        
        $seeds = Seed::search($search_text)->options([
            'attributesToHighlight' => ['name'],
            'attributesToRetrieve' => ['id', 'name', 'url', 'thumbnail'],
        ])->take(7)->raw();

        return view('search', [
            'seeds' => $seeds,
            'query' => $search_text
        ]);
    }
}

Please or to participate in this conversation.