You can follow this example for scout full text search in laravel 6, laravel 7, laravel 8 and laravel 9 project.
Laravel provide us several new feature and introduce new packages. Laravel provide Scout Package for full text search from your Model. If you require to add full text search function in your laravel application then you have to choose scout package for to do.
In this example i going to explain step by step, so you can simple understand and use it in your laravel project. In this tutorial i give from scratch so let's follow simple few step and implement full text search function in your laravel application.
Preview:
Step 1: Install Laravel Application
In this step, if you haven't laravel application setup then we have to get fresh laravel application. So run bellow command and get clean fresh laravel application.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install Packages
In this step we have to add two packages, there are listed bellow:
1)laravel/scout
2)algolia/algoliasearch-client-php
Ok, so first we will install "laravel/scout" package by following command:
composer require laravel/scout
Now we have to publish configure file by using bellow command, After this command run, you will find new file scout.php in config folder and we will set configure details on it, so let's run bellow command:
Ok, now we have to set id and secret of algolia, so first you have to create new account in algolia.com. So if you haven't account on algolia.com site then click here and create new account : algolia.com.
Ok, after login we have to get application id and secret so click here and open that web page: GET APP ID and SECRET. After open you will see bellow screen and copy your id and secret.
Ok, now open your .env file and paste id and secret like as bellow:
In this step we have to create migration for items table using Laravel php artisan command, so first fire bellow command:
php artisan make:migration create_items_table
After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create items table.
use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateItemsTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('items', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop("items"); }}
After create "items" table you should create Item model for items, so first create file in this path app/Item.php and put bellow content in item.php file:
app/Item.php
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use Laravel\Scout\Searchable;class Item extends Model{ use Searchable; public $fillable = ['title']; /** * Get the index name for the model. * * @return string */ public function searchableAs() { return 'items_index'; }}
Step 5: Add New Route
In this is step we need to create routes for add new items and listing. so open your routes/web.php file and add following route.
In this step, now we should create new controller as ItemSearchController in this path app/Http/Controllers/ItemSearchController.php. this controller will manage all listing items and add new item request and return response, so put bellow content in controller file:
app/Http/Controllers/ItemSearchController.php
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Http\Requests;use App\Item;class ItemSearchController extends Controller{ /** * Get the index name for the model. * * @return string */ public function index(Request $request) { if($request->has('titlesearch')){ $items = Item::search($request->titlesearch) ->paginate(6); }else{ $items = Item::paginate(6); } return view('item-search',compact('items')); } /** * Get the index name for the model. * * @return string */ public function create(Request $request) { $this->validate($request,['title'=>'required']); $items = Item::create($request->all()); return back(); }}
Step 7: Create View
In Last step, let's create item-search.blade.php(resources/views/item-search.blade.php) for layout and we will write design code here and put following code:
By Hardik Savani January 12, 2020 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenIn this tutorial, i will give you step by step example of laravel scout full text search. i will show you how to create full text search using laravel scout algolia.