Searching for users with Laravel Scout and TNTSearch
Tags: laravel, scout, search, tntsearch
Published: 2021-03-11 09:57:58

---
title: Searching for users with Laravel Scout and TNTSearch
published: true
description: Creating a powerful user search experience with Laravel Scout and TNTSearch
tags: laravel, scout, search, tntsearch
cover_image: https://i.imgur.com/E8Gd1Is.jpg
canonical_url: https://tnt.studio/searching-for-users-with-laravel-scout-and-tntsearch
---

In almost every web application, a common requirement is a search feature.
Clients are spoiled by Google and other search engines and expect a powerful search experience in their products. They don't realize the complexity behind it and just want it to work. Like they are used to.

This tutorial will cover how to search your `users` table and sequentially make yourself or your clients happy.

It's most likely that your application has a `users` table, and the client wants you to search over all the fields it contains. You'll probably come up with a solution that looks like this:

![](https://i.imgur.com/yvlcSAi.png)

This will certainly work for one keyword, but you'll face problems when you try to search for multiple keywords like `Taylor Otwell`. So you dig deeper into your toolbox and write something like:

![](https://i.imgur.com/jRCcPP2.png)

As you can see, this will cover the case `Taylor Otwell` but will have several downsides. This approach doesn't scale very well. If the wildcard `%` operator is both on the left and right side of the query `%query%`, the database's internal index cannot be leveraged, which means that the DB engine needs to go through every single row to see if there's a match. As you may know, that isn't nice... not to mention slow!

If you introduce more fields, you'll have even more permutations in your code. You'll end up with a non performant and non readable query.

The solution to this very problem comes in a [package](https://github.com/teamtnt/tntsearch) written in pure PHP that deals with this stuff and lets you do some cool things. 

Laravel also introduced a driver based solution for full-text search which works nicely with TNTSearch.

### Requirements

Lets see what the requirements are.

* Composer
* PHP >= 7.1
* PDO PHP Extension
* SQLite PHP Extension
* mbstring PHP Extension
* Laravel Framework 5.6+
* TNTSearch
* Laravecl Scout

This is a pretty standard stack, and most of the hosting providers will come with all of the requirements. If not, you may consider switching to [Digital Ocean](https://m.do.co/c/ddfc227b7d18)


### Installation

The installation process is easy and can be done in a couple of simple steps

`composer require teamtnt/laravel-scout-tntsearch-driver`

After that, add the service provider to `app/config/app.php`:

![](https://i.imgur.com/Yz1gEH2.png)

Add  `SCOUT_DRIVER=tntsearch` to your `.env` file

Then you should publish the `scout.php` configuration file to your config directory

![](https://i.imgur.com/NKl3zhO.png)

In your `config/scout.php` add:

![](https://i.imgur.com/K4OLfvA.png)


### Creating the index

The first thing is creating the index so that we can search against it in milliseconds. You'll be amazed how fast it is.

We'll create a laravel command that will do the indexing for us:

![](https://i.imgur.com/m7aeZ82.png)

Once we have this command, we can run `php artisan index:users`. This will create a file in your
storage folder called `users.index`. Now we are able to do queries against it.

Another approach, and a more common one to achieve the same thing, is to add a searchable trait to your model:

![](https://i.imgur.com/2qIKmE9.png)

And then running:

`php artisan scout:import App\\Post`

That's it. The index has been created.

### Performing a search

Performing the search is simple. We'll do this in our `UserController`:

![](https://i.imgur.com/mYatfYF.png)

This controller assumes that you have a simple search form which submits a query in 
an input field called `q`.

Updating the index manually isn't necessary because scout will do it automatically.

What about scaling you may ask? Will it handle more than 100k users? Don't worry, it will scale fine even if you have millions of users. The index itself is a `sqlite` database and is optimized for millions of records.

### Conclusion
 
As you saw from the tutorial, adding a full-text search to your project is pretty straightforward. We covered only the tip of the iceberg here. [TNTSearch](https://github.com/teamtnt/tntsearch) is a powerful engine that can do a lot of stuff - even classification if that's something you might need. For more info, you can check out the GitHub documentation. 

We also created a [cool demo](https://airports.tnt.studio/) for you so that you see the engine's power in action. It's an airport search that displays the results directly on the map in real-time. 
If you like it, leave a mention on twitter, and we might create a tutorial on how to do it.

This article was originally published on [tnt.studio](https://tnt.studio/searching-for-users-with-laravel-scout-and-tntsearch)