Overview
In this guide, you can learn how to run a text query by using Laravel MongoDB.
You can use a text query to retrieve documents that contain a term or a phrase in a specified field. A term is a sequence of characters that excludes whitespace characters. A phrase is a sequence of terms with any number of whitespace characters.
This guide describes the Eloquent model methods that you can use to search text and provides examples. To learn more about Eloquent models in the Laravel Integration, see the Eloquent Models section.
Before You Get Started
To run the code examples in this guide, complete the Quick Start tutorial. This tutorial provides instructions on setting up a MongoDB Atlas instance with sample data and creating the following files in your Laravel web application:
- Movie.phpfile, which contains a- Moviemodel to represent documents in the- moviescollection
- MovieController.phpfile, which contains a- show()function to run database operations
- browse_movies.blade.phpfile, which contains HTML code to display the results of database operations
The following sections describe how to edit the files in your Laravel application to run the find operation code examples and view the expected output.
Search Text Fields
Before you can perform a text query, you must create a text index on the text-valued field. To learn more about creating indexes, see the Manage Indexes section of the Schema Builder guide.
You can perform a text query by using the $text operator followed
by the $search field in your query filter that you pass to the
where() method. The $text operator performs a text query on the
text-indexed fields. The $search field specifies the text to search for.
After building your query by using the where() method, chain the get()
method to retrieve the query results.
This example calls the where() method on the Movie Eloquent model to
retrieve documents in which the plot field contains the phrase
"love story". To perform this text query, the collection must have
a text index on the plot field.
Use the following syntax to specify the query:
$movies = Movie::where('$text', ['$search' => '"love story"'])     ->get(); 
To see the query results in the browse_movies view, edit the show() function
in the MovieController.php file to resemble the following code:
class MovieController {     public function show()     {          $movies = Movie::where('$text', ['$search' => '"love story"'])              ->get();          return view('browse_movies', [              'movies' => $movies          ]);     } } 
Title: Cafè de Flore Year: 2011 Runtime: 120 IMDB Rating: 7.4 IMDB Votes: 9663 Plot: A love story between a man and woman ... Title: Paheli Year: 2005 Runtime: 140 IMDB Rating: 6.7 IMDB Votes: 8909 Plot: A folk tale - supernatural love story about a ghost ... Title: Por un puèado de besos Year: 2014 Runtime: 98 IMDB Rating: 6.1 IMDB Votes: 223 Plot: A girl. A boy. A love story ... ... 
Search Score
A text query assigns a numerical text score to indicate how closely
each result matches the string in your query filter. You can sort the
results by relevance by using the orderBy() method to sort on the
textScore metadata field. You can access this metadata by using the
$meta operator:
$movies = Movie::where('$text', ['$search' => '"love story"'])     ->orderBy('score', ['$meta' => 'textScore'])     ->get(); 
Tip
To learn more about the orderBy() method, see the
Sort Query Results section of the Modify Query Output guide.
Additional Information
To view runnable code examples that demonstrate how to perform find operations by using the Laravel Integration, see the following usage examples:
To learn how to retrieve data based on filter criteria, see the Retrieve Data guide.