Laravel和Algolia软件包有没有办法更新到索引,而不是所有字段?
您可以在模型中使用 getAlgoliaRecord()
方法,然后从中返回数组,并带有要索引的属性。
示例:
use IlluminateDatabaseEloquentModel;
class Contact extends Model
{
use AlgoliaEloquentTrait;
public function getAlgoliaRecord()
{
return [
'indexedAttribute' => $this->indexedAttribute,
'otherIindexedAttribute' => $this->otherIindexedAttribute,
'nextIndexedAttribute' => $this->nextIndexedAttribute,
];
}
}
@janpetr答案是正确的,但仅适用于laravel 4。
对于Laravel 5.3及以上,
文档中提到
默认情况下,给定模型的整个toarray形式将持续到其搜索索引。如果您想自定义与搜索索引同步的数据,则可以覆盖模型上的tosearchablearearray方法。
<?php
namespace App;
use LaravelScoutSearchable;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
use Searchable;
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
// Customize array...
$array = [
'post_name' => $this->post_name,
'post_author' => $this->author,
'publisher' => $this->publisher,
'publishing_date' => $this->published_at
];
return $array;
}
}