wasChanged('status'); $originalLocale = app()->getLocale(); $locales = $statusChanged ? array_keys(LaravelLocalization::getSupportedLocales()) : [$originalLocale]; // Loop on all locales to update indexes when global fields like status is updated, // Otherwise, on current locale. collect($locales)->map(function($locale) use ($keyPrefix, $model, $usesSoftDeletes) { // Set the locale to alter the right ms index. app()->setLocale($locale); $indexId = "{$keyPrefix}_{$model->getKey()}"; // Remove model from meilisearch index. if ($usesSoftDeletes && method_exists($model, 'trashed') && $model->trashed()) { SearchIndex::make([ 'type' => $keyPrefix, 'model_id' => $model->getKey(), ])->unsearchable(); return; } $msIndex = $model->toSearchDocument(); // Add model to the meilisearch index if conditions are true, remove from it if not. if ($msIndex) { $msIndex->searchable(); } else { SearchIndex::make([ 'type' => $keyPrefix, 'model_id' => $model->getKey(), ])->unsearchable(); } }); // Reset the locale with the original. app()->setLocale($originalLocale); }); static::deleted(function ($model) use ($keyPrefix) { $originalLocale = app()->getLocale(); $locales = array_keys(LaravelLocalization::getSupportedLocales()); // Loop on all locales to update indexes when model is deleted (softdeleted or not). collect($locales)->map(function($locale) use ($keyPrefix, $model) { // Set the locale to alter the right ms index. app()->setLocale($locale); $indexId = "{$keyPrefix}_{$model->getKey()}"; // Remove model from meilisearch index. SearchIndex::make([ 'type' => $keyPrefix, 'model_id' => $model->getKey(), ])->unsearchable(); }); // Reset the locale with the original. app()->setLocale($originalLocale); }); if ($usesSoftDeletes) { static::restored(function ($model) { $originalLocale = app()->getLocale(); $locales = array_keys(LaravelLocalization::getSupportedLocales()); // Loop on all locales to update indexes when model is restored. collect($locales)->map(function($locale) use ($model) { // Set the locale to alter the right ms index. app()->setLocale($locale); // Add model to the meilisearch index if conditions are true. $msIndex = $model->toSearchDocument(); if ($msIndex) { $msIndex->searchable(); } }); // Reset the locale with the original. app()->setLocale($originalLocale); }); } } }