laravel scout在更新时暂时禁用toSearchableArray

polhcujo  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(117)

是否有可能禁用toSearchableArray当做更新到一个记录或是否有任何方法来只更新特定领域的记录在我的搜索索引?
例如:

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

字符串
现在唯一的问题是,每次我更新一个记录时,它也会重置它的uploaded_at时间戳,并重新加载关系,这是一个我不需要的查询,因为它已经在我创建项目时设置了。
那么有没有什么方法可以暂时禁用toSearchableArray呢?我只需要更新索引行中的几个字段,这样就不需要在toSearchableArray中删除所有内容了。
像波纹管只更新标题,然后更新标题在我的algolia索引没有重置uploaded_at或再次加载category关系

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

e4eetjau

e4eetjau1#

你可以使用laravel scout中的unsearchable函数。

$modal->unsearchable();

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

$modal->save()

字符串
这样当你保存或者更新的时候它就不会同步到algolia了。如果你再次想要同步模型到algolia,你可以调用searchable方法,如下所示。

$modal->searchable();

inb24sb2

inb24sb22#

要完全禁用,请添加服务提供商:

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

字符串
暂时禁用:

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

相关问题