laravel search using scout without algoliasearch client


step 0 : if you not craft laravel project . install new one

laravel new your-project

step 1 : install scout to your project .

composer require laravel/scout

step 2 : add the ScoutServiceProvider to the providers array of your config/app.php

'providers' => [
   ...
   
   Laravel\Scout\ScoutServiceProvider::class,

   ...
],

step 3 : publish config file from vendor of project

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

step 4 : install /laravel-scout-tntsearch-driver to your project

composer require teamtnt/laravel-scout-tntsearch-driver

step 5 : add service provider like you do with scout

'providers' => [
    ...    

    TeamTNT\Scout\TNTSearchScoutServiceProvider::class,

    ...
],

step 6 : add config of tntsearch to your scout.php in config/app.php and comment algolia setting

    // 'algolia' => [
    //     'id' => env('ALGOLIA_APP_ID', ''),
    //     'secret' => env('ALGOLIA_SECRET', ''),
    // ],

    'tntsearch' => [
        'storage'  => storage_path(), //place where the index files will be stored
        'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
        'fuzzy' => [
            'prefix_length' => 2,
            'max_expansions' => 50,
            'distance' => 2
        ],
        'asYouType' => false,
    ],

done ! your config finally complete. now let go to your model.php to add service

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class yourmodelname extends Model
{
    use Searchable;

    public $fillable = ['title'];

    public function toSearchableArray()
    {
        $array = $this->toArray();

        // Customize array...

        return $array;
    }
 
}

ok ! finally config in your controller or anywhere you want to use it

namespace App\Http\Controllers\frontend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\yourmodel;

class nameController extends Controller
{

   public function searchPost($title){
        $post = Post::search($title);

        return view('search',[
            'result' => $post
        ]);
    }

}

for more info about tntsearch please go to laravel-scout-tntsearch-driver

 

Share this Post:

Related Posts:

www.000webhost.com