Ahmed essam
Ahmed essam

Reputation: 357

Laravel switch to eloquent search if Scout and Algolia stopped

I am using Scout and Algolia for creating my search engine and using the community plan which is limited and free.

I am wondering is there a way to switch back to the normal eloquent using WHERE LIKE if a problem happened in Scout or Algolia like subscription?

Upvotes: 2

Views: 768

Answers (2)

Serhii Shliakhov
Serhii Shliakhov

Reputation: 2769

You can decorate algolia engine with fallback decorator and switch to fallback if an error occurs.

class AppServiceProvider extends ServiceProvider
{
    /**
     * {@inheritdoc}
     */
    public function boot(): void
    {

        resolve(EngineManager::class)->extend(AppSearchEngine::class, function () {
            $origin = new AlgoliaEngine();
            $fallback = new MySQLEngine();
            return new FallbackEngine($origin, $fallback);
        });
    }
}

class FallbackEngine extends Engine {
...
    public function search(Builder $builder)
    {
        try {
            return $this->origin->search();
        } ($e \Excheption) {
            return $this->fallback->search();
        }
    }
...
}

Upvotes: 4

hemant
hemant

Reputation: 154

Yes you can use normal query to search results in laravel.

ModelName::query()
   ->where('name', 'LIKE', "%{$searchTerm}%") 
   ->orWhere('email', 'LIKE', "%{$searchTerm}%") 
   ->get();

Or Simply build a macro to search in laravel

https://freek.dev/1182-searching-models-using-a-where-like-query-in-laravel

Upvotes: -2

Related Questions