3

I have an app that I am writing with Laravel. I am still fairly new with the framework and don't understand most of it. I am using Algolia as the search engine with Laravel's Scout. In the models you add use Searchable, a trait, and the records are automatically passed to Algolia, which is cool. I am trying to put a simple statement if (App::environment('local'))" exit scout, just so we are not sending our development data to Algolia. Scout will also throw an exception if I run out of the hacker level of 10,000 records a Algolia.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Chris Edwards
  • 454
  • 1
  • 9
  • 22

4 Answers4

13

In your local .env add

SCOUT_DRIVER=null

In production add

SCOUT_DRIVER=algolia

In config/scout.php add

'driver' => env('SCOUT_DRIVER', 'null')

Automatically it will be ignored in local but work in production. This is just a suggestion. Try to adapt it to your specific context.

EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
  • 1
    Almost perfect! just drop the "SCOUT_DRIVER=null" in the local .env. Otherwise it fails when the driver is checked. Thanks, Chris – Chris Edwards Jan 17 '17 at 14:41
  • may I ask you to have a look at a Laravel search related question here: https://stackoverflow.com/questions/76485513/laravel-search-with-multiple-keywords-against-multiple-columns-with-the-search ? – Istiaque Ahmed Jun 15 '23 at 22:09
4

On your local environment you can call YourModel::disableSearchSyncing(), which will prevent this model from pushing data to Algolia.

The reverse to this method is YourModel::enableSeachSyncing(), but the search is enabled by default, so usually there is no need to use it.

Jan Petr
  • 685
  • 5
  • 11
0

Per the Laravel 5.3 documentation:

  1. Set the environment the in the .env file:

    APP_ENV=local
    
  2. Determine the current environment:

    $environment = App::environment();
    
  3. Check on $environment and return true:

    if (App::environment('local')) {
        // The environment is local
    }
    if (App::environment('local', 'staging')) {
        // The environment is either local OR staging...
    }
    
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
0

None of above solution works I suggest you to check in your toSearchableArray() method inside your User Model. If you try to set the SCOUT_DRIVER=null in local environment then you will face an error because your application tends to push to Algolia in any environment.

Try to do this instead:

public function toSearchableArray()
{
    if (! app()->isLocal()) {
        return [
            'username' => $this->username,
            'age'      => (string) $this->age,
            // and so on ...
        ];
    }
}
Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43