Hi!
In angolia I have list of Variant objects.
Inside variant object I have products objects. Relation between product and variant belongsTo.
I each product I have brand object. Relation between brand and product: “hasMany”
I update my brand using following code:
$brand->name = "brand name";
$brand->save();
In Brand model I added following code as in documentation: Customize Searchable Data | Laravel | Algolia
public static function boot()
{
parent::boot();
static::saved(function ($model) {
foreach ($model->products as $product) {
$product->searchable();
}
});
}
My relation in Brand model:
public function products()
{
return $this->hasMany(Product::class, 'brand_id', 'id');
}
I debugged static::saved method and it works, but not update records in angolia
But If I change products relation from hasMany to hasOne:
public function products()
{
return $this->hasOne(Product::class, 'brand_id', 'id');
}
and add:
protected $touches = ['products'];
It works, but only for one record, it doesn’t update add brands with the same id in all variants objects