user2722667
user2722667

Reputation: 8661

laravel scout temporary disable toSearchableArray when doing updates

is it possible to disable toSearchableArray when doing updates to a record or is there any way to only update specific fields in a record in my search index?

Eg:

public function toSearchableArray()
{
          $item = $this->toArray();
        $item['title'] = $this->title;
         ...
         ...
         ...
         $item['category'] = $this->category->category_name;
         $item['uploaded_at'] = Carbon::now('America/Montreal')->timestamp;
}

The only problem now is each time I update a record it also resets its uploaded_at timestamp and re-loads the relationship which is one more query I dont need since it already has it set when I created the item.

So is there any way I can temporary disable toSearchableArray ? I only need to update a few fields in in the row in my index so there is no need to rerun everything in toSearchableArray

Like bellow only update the title and then update the title in my algolia index without reseting uploaded_at or loading the category relation again

$order = App\Order::find(1);
$order->title = 'a new title'
$order->save();

Upvotes: 1

Views: 2129

Answers (2)

yaroslawww
yaroslawww

Reputation: 1088

To fully disable, add in service provider:

\App\Models\Content::disableSearchSyncing();

Temporary disable:

\App\Models\Content::disableSearchSyncing();
\App\Models\Content::create(['data' => 'Foo bar']);
\App\Models\Content::enableSearchSyncing();

Upvotes: 1

You can use, unsearchable function available in laravel scout.

$modal->unsearchable();

//etc.....
//Finally save the modal

$modal->save()

This way when you save or update it's won't sync to algolia. If You again want to sync the model to algolia you may call searchable method as shown below.

$modal->searchable();

Upvotes: 0

Related Questions