Model delete has no effect on indexed related model

I have a model Topics and a model SubTopic which is related to Topics. I indexed my Topics with the relation Subtopic and that works perfectly. The problem is when I want to delete my subtopics than the related topic won’t update. Like so:

$subtopic->delete();

Has no effect on my indexed Topic model. Maybe some code of my models.

Topic Model

class Topic extends Model
{
    use Searchable;

    public function toSearchableArray()
    {

        $this->subtopics;

        $record = $this->toArray();

        $record = $this->transform($record);

        return $record;
    }

SubTopic Model

class SubTopic extends Model
{
    use Searchable;

    protected $touches = ['topic'];

Added $touches to update the Topics Model when a SubTopic is changed.

Maybe I overlooked something in the docs.

Best

1 Like

Can you confirm the the topic relation is a belongsTo and not hasOne?
Indeed, the touch features only works with belongsTo and belongsToMany.

Related documentation:
https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/?language=php#using-touch-feature

Yep thats right.

  /**
    * Get the topic that owns the subtopic.
    */
    public function topic()
    {
        return $this->belongsTo('App\Topic');
    }

Does it work when you update a subtopic? I’m wondering if touch works with delete operations.

Otherwise, I’d recommend using the other way with callbacks in the boot methods

Yep if I update a subtopic it works perfectly!

Tried now to do it like in the link provided but no sucess, it deletes all subtopics and the corresponding topic. Maybe I am doing it not right, but only found the unsearchable method in the Searchable class.

  public static function boot()
    {
       parent::boot();

       static::deleted(function ($model) {
         $model->topic->unsearchable();
       });
    }

Any further advice to get this working? The above doesn’t work for some reasong. It feels like the index gets updated before the model gets deleted…

best

Maybe add this code to your SubTopic model:

  public static function boot()
    {
       parent::boot();

       static::deleted(function ($model) {
         $model->topic->searchable(); // Searchable will reindex the topic model, therefore making all the subtopics up-to-date.
       });
    }

Thanks for your answer but it changes nothing, problem persists. I really dont know why.

My SubTopic.php looks like

  public static function boot()
    {
        parent::boot();
        static::deleted(function($model) { // before delete() method call this
             $model->topic->unsearchable();
        });

        static::deleted(function ($model) {
         $model->topic->searchable(); // Searchable will reindex the topic model, therefore making all the subtopics up-to-date.
        });
    }

Tried it also with only one of these methods. Tried it also with static::deleting.

Best

Just to make sure I got your problem. This is what you expected:

  1. Two indexes Topic and SubTopic.
  2. One record of Topic contains the topic itself, and the SubTopics inside because of the relation.
  3. When you delete a sub-topic, you want to reindex the concerned topic record.

Correct?

I dont know if I get you right. I have one index Topic with subtopics inside because of relation and if I delete a subtopic I want it to be deleted in the corresponding topic index.

Now I want to delete the subtopic with id: “57” in the subtopics array.

We are in sync. That’s what I meant. Normally this code on the SubTopic Model should work:

  1. Solution one:

Put this on the SubTopic:

  public static function boot()
    {
        parent::boot();

        static::deleted(function ($model) {
         $model->topic->searchable(); // reindex the topic model, therefore making all its subtopics up-to-date.
        });
    }

Solution 2:

Put this on the SubTopic:

  public static function boot()
    {
        parent::boot();

        static::deleted(function ($model) {
         $model->topic->update(); // Update and reindex the topic model, therefore making all its subtopics up-to-date.
        });
    }
  1. Solution three:

Put this in your SubTopics:

protected $touches = ['topic'];

If this don’t work, I think you should debug a little bit or/and check the logs.

Thanks,

I had this already in my SubTopic.php

protected $touches = ['topic'];

removed it now and defined static method for every action and it seems to work, but is this the expected behaviour? My Subtopic.php looks now like so:

public static function boot()
    {
        parent::boot();

        static::deleted(function ($model) {
          $model->topic->update();
        });

        static::saved(function ($model) {
          $model->topic->update();
        });

        static::updated(function ($model) {
          $model->topic->update();
        });
    }

Maybe in the docs should be stated that this

only works for the save and update method?

Best

I think in reality, only one of those events are needed:

        static::deleted(function ($model) {
          $model->topic->update();
        });

        static::saved(function ($model) {
          $model->topic->update();
        });

        static::updated(function ($model) {
          $model->topic->update();
        });

Can you comment to see the one that actually works?

I need these two methods

//For deleting subtopics
static::deleted(function ($model) {
          $model->topic->update();
        });
// For updating and saving subtopics
        static::saved(function ($model) {
          $model->topic->update();
        });
1 Like