{"id":1556,"date":"2024-09-06T18:00:25","date_gmt":"2024-09-06T18:00:25","guid":{"rendered":"https:\/\/debugspot.com\/?p=1556"},"modified":"2024-09-06T18:36:16","modified_gmt":"2024-09-06T18:36:16","slug":"laravel-scout-laravel-11-complete-tutorial-example","status":"publish","type":"post","link":"https:\/\/debugspot.com\/blogs\/laravel-scout-laravel-11-complete-tutorial-example\/","title":{"rendered":"Laravel Scout in Laravel 11: A Comprehensive Tutorial with Example"},"content":{"rendered":"<p>Learn how to set up Laravel Scout in Laravel 11 with this detailed guide. Explore the installation process, configuration options, and search driver setup to boost your application&#8217;s search functionality.<\/p>\n<p>In today&#8217;s web applications, having effective search features is key to improving user experience. Laravel Scout is a handy tool that works with Laravel 11 to offer full-text search capabilities. This guide will walk you through the process of setting up Laravel Scout, configuring it with Meilisearch, and refining your search setup with advanced techniques.<\/p>\n<h3 class=\"wp-block-heading\">Step 1: Install Laravel Scout<\/h3>\n<p>To begin, make sure you have Laravel installed on your system. You can add Laravel Scout to your project using Composer. Open your terminal and run the following command:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> composer require laravel\/scout <\/code><\/pre>\n<p>This command will add Laravel Scout to your project, allowing you to integrate it with your Laravel application.<\/p>\n<h3 class=\"wp-block-heading\">Step 2: Configure Scout<\/h3>\n<p>After the installation is finished, you&#8217;ll need to publish the Scout configuration file to customize its settings according to your needs.<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> php artisan vendor:publish --provider=\"Laravel\\Scout\\ScoutServiceProvider\" <\/code><\/pre>\n<p>This will generate a config\/scout.php file where you can customize the default settings to fit your requirements.<\/p>\n<h3 class=\"wp-block-heading\">Step 3: Choose a Search Driver<\/h3>\n<p>Laravel Scout works with different search engines, with Algolia being the default option. To use a different search driver, simply update the config\/scout.php file.<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> 'driver' => env('SCOUT_DRIVER', 'algolia'), <\/code><\/pre>\n<p>For this tutorial, we will use Meilisearch. Update your .env file to set:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> SCOUT_DRIVER=meilisearch <\/code><\/pre>\n<h3 class=\"wp-block-heading\">Step 4: Set Up Meilisearch<\/h3>\n<p>If you opt for Meilisearch, follow the official Meilisearch documentation to install and configure it. After installation, configure Meilisearch in your .env file:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> MEILISEARCH_HOST=http:\/\/127.0.0.1:7700 <\/code><\/pre>\n<p>Ensure you have set SCOUT_DRIVER=meilisearch to use Meilisearch as your search engine.<\/p>\n<h3 class=\"wp-block-heading\">Step 5: Add the Searchable Trait to Your Model<\/h3>\n<p>To enable search functionality for an Eloquent model, use the Searchable trait. For example, modify your Post model as follows:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> \r\n    namespace App\\Models;\r\n\r\n    use Illuminate\\Database\\Eloquent\\Model;\r\n    use Laravel\\Scout\\Searchable;\r\n    \r\n    class Post extends Model\r\n    {\r\n        use Searchable;\r\n    \r\n        \/\/ Additional model code...\r\n    }\r\n        \r\n<\/code><\/pre>\n<p>Adding this trait allows Laravel Scout to index and search the Post model&#8217;s records. <\/p>\n<h3 class=\"wp-block-heading\">Step 6: Index Your Data<\/h3>\n<p>Populate your search index by running the scout:import Artisan command:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> php artisan scout:import \"App\\Models\\Post\" <\/code><\/pre>\n<p>This command syncs all existing records of the Post model with your search engine, making them searchable.<\/p>\n<h3 class=\"wp-block-heading\">Step 7: Performing Searches<\/h3>\n<p>With indexing complete, you can perform searches on your model. Here&#8217;s how to search for posts by a keyword:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> $posts = App\\Models\\Post::search('keyword')->get(); <\/code><\/pre>\n<p>This query retrieves all posts that match the specified keyword.<\/p>\n<h3 class=\"wp-block-heading\">Step 8: Customizing Search Behavior<\/h3>\n<p>To customize how models are indexed, override the toSearchableArray method in your model:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\n    class Post extends Model\r\n    {\r\n        use Searchable;\r\n    \r\n        public function toSearchableArray()\r\n        {\r\n            $array = $this->toArray();\r\n    \r\n            \/\/ Customize array...\r\n    \r\n            return $array;\r\n        }\r\n    }    \r\n<\/code><\/pre>\n<p>This method allows you to tailor the data indexed by Scout.<\/p>\n<h3 class=\"wp-block-heading\">Step 9: Handling Indexing in Background<\/h3>\n<p>To handle indexing in the background, use Laravel&#8217;s queue system. Enable this by setting SCOUT_QUEUE to true in your .env file:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> SCOUT_QUEUE=true <\/code><\/pre>\n<p>Ensure your queue worker is running with:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> php artisan queue:work <\/code><\/pre>\n<p>This setup enhances performance by handling indexing tasks in the background.<\/p>\n<h3 class=\"wp-block-heading\">Step 10: Testing Your Search Implementation<\/h3>\n<p>Make sure your search feature performs correctly by writing tests. Here\u2019s a simple example using PHPUnit:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> \r\n    use Illuminate\\Foundation\\Testing\\RefreshDatabase;\r\n    use App\\Models\\Post;\r\n    \r\n    class SearchTest extends TestCase\r\n    {\r\n        use RefreshDatabase;\r\n    \r\n        public function test_search_returns_expected_results()\r\n        {\r\n            Post::factory()->create(['title' => 'Laravel Scout Tutorial']);\r\n            Post::factory()->create(['title' => 'Another Post']);\r\n    \r\n            $results = Post::search('Laravel')->get();\r\n    \r\n            $this->assertCount(1, $results);\r\n        }\r\n    }        \r\n<\/code><\/pre>\n<p>Testing helps verify that your search setup works properly and delivers the results you expect.<\/p>\n<p>For More detail you can refer this <a href=\"https:\/\/laravel.com\/docs\/11.x\/scout\" target=\"_blank\" rel=\"noopener\"><strong>Laravel Scout<\/strong><\/a> Document<\/p>\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n<p>By following this comprehensive guide, you should now have a fully functional Laravel Scout setup in your Laravel 11 project. From installation to advanced usage, Laravel Scout enhances your application&#8217;s search capabilities, making it easier for users to find relevant information quickly and efficiently. Explore additional features and tailor Scout to fit your application&#8217;s specific needs.<\/p>\n<h5>You may also find interesting:<\/h5>\n<p><a title=\"Laravel Scout in Laravel 11: A Comprehensive Tutorial with Example\" href=\"https:\/\/debugspot.com\/blogs\/integrate-google-calendar-with-laravel\/\" target=\"_blank\" rel=\"noopener\">Laravel Scout in Laravel 11: A Comprehensive Tutorial with Example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to set up Laravel Scout in Laravel 11 with this detailed guide. Explore the installation process, configuration options, and search driver setup to boost your application&#8217;s search functionality. In today&#8217;s web applications, having effective search features is key to improving user experience. Laravel Scout is a handy tool that works with Laravel 11 to offer full-text search capabilities. This guide will walk you through the process of setting up Laravel Scout, configuring it with Meilisearch, and refining your search setup with advanced techniques. Step 1: Install Laravel Scout To begin, make sure you have Laravel installed on your system. You can add Laravel Scout to your project using Composer. Open your terminal and run the following command: composer require laravel\/scout This command will add Laravel Scout to your project, allowing you to integrate it with your Laravel application. Step 2: Configure Scout After the installation is finished, you&#8217;ll need to publish the Scout configuration file to customize its settings according to your needs. php artisan vendor:publish &#8211;provider=&#8221;Laravel\\Scout\\ScoutServiceProvider&#8221; This will generate a config\/scout.php file where you can customize the default settings to fit your requirements. Step 3: Choose a Search Driver Laravel Scout works with different search engines, with &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_lock_modified_date":false,"footnotes":""},"categories":[3],"tags":[46,54,53],"class_list":["post-1556","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel","tag-laravel-scout","tag-laravel11"],"acf":[],"_links":{"self":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1556"}],"collection":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/comments?post=1556"}],"version-history":[{"count":5,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1556\/revisions"}],"predecessor-version":[{"id":1561,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1556\/revisions\/1561"}],"wp:attachment":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media?parent=1556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/categories?post=1556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/tags?post=1556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}