MongoDB (not MongoDB Atlas) engine for the Laravel Scout search.
composer require qrstuff/scout-mongodb
Before following this guide, make sure you have installed and set up laravel/scout in your project already.
In your config/database.php
, add the mongodb connection:
return [
// other stuff
'connections' => [
// other stuff
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('MONGODB_URL'),
'database' => env('MONGODB_DATABASE', 'example'),
],
],
// other stuff
];
In your config/scout.php
, add the mongodb definition:
return [
// other stuff
'mongodb' => [
'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'),
'index-settings' => [
// App\Models\User::class => [
// 'searchableAttributes'=> ['name', 'email', 'phone'],
// 'filterableAttributes'=> [['country' => 1]], // 1 = ASC, 2 = DESC
// ],
],
],
// other stuff
];
Then add the Searchable
trait your model classes as follows:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Customer extends Model
{
use Searchable; // include the trait
}
You can now search across your models as below:
use App\Models\Customer;
$customersInIndia = Customer::search('vpz')->where('country', 'IN')->get();
See LICENSE file.