Using Laravel Scout with global query scopes

I recently implemented Laravel Scout, Laravel’s own full text search package, into one of my projects. One of the models which Scout was enabled for had global query scopes implemented. They are used to defining specific query constraints for all queries run on this model. You must be careful with defining them, as they affect everything done with the model.

Let’s say, you have an Article model, which has some database fields that should be indexed, and a language field which is used to query articles per language, because our site is multilingual.

A global query scope is defined as a separate class and then added to the model. The following scope will add a clause to the query, in which the language field is specified, and the current application locale is used.

To make the scope usable for every model, I decided to make it depend on the Model class itself, instead of a specific model. To prevent any issues with relations in other queries, the scope uses a table prefix, which we can get by using . In our case with the Article model, the where() clause would look like this: .

To enable this global scope, add it to the models method:

If you run a query like now, you will get all articles which match the current language.

Importing models with Scout

After installing Scout and preparing the model for it, you must import all models into the index to be able to search them. While testing the import of my models, I noticed that only a fraction of models has been imported. Actually, global scopes are the reason. Because they affect all queries made to the model, Scout is not able to retrieve all entries for the import.

Disabling global query scopes for the Scout import

The Searchable trait which is added to models to enable Scout, ships with a function called . This method is used to get all model entries for the import. By default, the method is protected and can't be accessed in any way while running the import. However, you can override it inside your model. If you do this, you can add the method to the query to disable all global scopes for the import query.

After adding this to the model, I was able to import all entries, and not just a fraction.

Global scopes while searching with Scout

Searching for entries with Scout is a bit special, because you can’t apply any regular database queries to the search by default and also don’t prepend any constraints to the search. However, Scout allows us to add simple clauses to the search. See the documentation of Scout for reference.
While testing the search, I was wondering why I got no results for my model despite having entered a string correctly. Of course my global scope was applied to the search query. After digging through the code for a while, I found a way to access the underlying database query builder.

Disable global scopes while searching

It is indeed possible to disable global scopes while running a search with scout. Let’s say you want to search an article for something.

Right now our global scope would affect the search and only return entries for like English, if you are on the English version of your site. If you want to search across all languages, modify the code like this:

The query method allows us to modify the query builder and disable all global scopes while searching for our articles.

This article was first published on my blog.

Web Developer | Creator of and InvoicePlane

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store