Yaroslav Domen
Yaroslav Domen

Reputation: 21

Updating model data in the meilisearch index when editing this model's links

Input data regarding the database:

Let's take as an example the relationship of a product with categories, which can be many:

public function categories(): BelongsToMany
{
    return $this->belongsToMany(Category::class);
}

Next, in the makeAllSearchableUsing() method, we define with which links we want to store the product model in the meilisearch index

protected function makeAllSearchableUsing($query): Builder
{
    return $query->with(['categories']);
}

After we load the data into the index of the meilisearch system, with further search, we will be given something like this answer (only the part necessary for the demonstration is left):

{
  id: 12615,
  name: "FORD chip...",
  slug: "...",
  categories: [
    {
      id: 41735,
      product_id: 12615
      name: "Ford...",
    },
    {
      id: 41739,
      product_id: 12615
      name: "Ford Alternate...",
    }
  ]
}

As you can see, the product categories (and, accordingly, all other links that we will store with it) are hardcoded into its data array.

The question is:

When updating the name of a category, I need to write the logic for updating product data myself, and in general any other models that have a relationship with categories and it is written in meilisearch (for example, in events, or in an observer), or this is possible in automatic mode, with some more advanced settings of this package ?

Thanks if anyone can help.

It's not just an inconvenience, but also the fact that if I change the name of a category, for example, I will have to pre-record several million products that have it listed.

Upvotes: 1

Views: 1125

Answers (1)

Farid Froozan
Farid Froozan

Reputation: 28

Do you use events or trait with auto model events like saving or updated like this link ?

Upvotes: 0

Related Questions