scout-extended icon indicating copy to clipboard operation
scout-extended copied to clipboard

Docs might warn about belongsToMany() and $touches

Open smknstd opened this issue 6 years ago • 9 comments

When using $touches with belongsToMany relation as mentionned in documentation, we can lack of update in the case of a deassociation.

With:

class Article extends Model
{
    use Searchable;

    protected $touches = ['authors'];

    public function authors()
    {
        return $this->belongsToMany(Authors::class);
    }
}

and in the scenario

$article->authors()->attach(1)
$article->authors()->attach(2)
...
$article->authors()->sync([1])

author 2 will not be updated in algolia. Maybe doc could mention this.

smknstd avatar Oct 29 '19 14:10 smknstd

Sounds a good idea, to clear understand the idea, can you write bellow a small snippet so a copy/past it into the docs?

nunomaduro avatar Oct 29 '19 15:10 nunomaduro

Handling deletion with belongsToMany

While using a belongsToMany relationship with a pivot table

class Article extends Model
{
    use Searchable;

    public function authors()
    {
        return $this->belongsToMany(Authors::class);
    }
}

class Author extends Model
{
    protected $touches = ['articles'];

    public function articles()
    {
        return $this->belongsToMany(Article::class);
    }
}

be aware that "touches" hook is not triggered when a link is removed between two related items (for example: $article->authors()->detach($author) or $article->authors()->sync([])). So you'll need to handle this additionally.

One way to do this, is to define a custom intermediate table model and hook on delete event to trigger change:

class Author extends Model
{
    protected $touches = ['articles'];

    public function articles()
    {
        return $this->belongsToMany(Article::class)->using(ArticleAuthor::class);
    }
}

class ArticleAuthor extends Pivot
{
    public static function boot()
    {
        parent::boot();
        static::deleting(function ($item) {
            Article::find($item->article_id)->touch();
        });
    }
}

smknstd avatar Oct 31 '19 16:10 smknstd

What do you think @nunomaduro ?

IMO the whole "Relationships section" would deserve a dedicated page in the docs.

smknstd avatar Oct 31 '19 17:10 smknstd

I will check it next Monday. Thanks for this!

nunomaduro avatar Nov 01 '19 10:11 nunomaduro

You are right. Can you add bellow this comment the documentation that should end up in the docs? Thank you for your work on this.

nunomaduro avatar Nov 04 '19 10:11 nunomaduro

I would simply move the https://www.algolia.com/doc/framework-integration/laravel/indexing/configure-searchable-data/?language=php#relationships to a dedicated page as it doesn't fit well in "Customize Searchable Data" in my opinion and it would be easier to navigate/find in the menu.

Being able to PRs the doc would have been a lot easier :)

smknstd avatar Nov 05 '19 15:11 smknstd

Sounds good. Going to mark this as docs, and handle it soon as possible.

nunomaduro avatar Nov 05 '19 16:11 nunomaduro

in the mean time, I made it a blog post: https://medium.com/code16/handling-changes-on-belongstomany-relations-with-laravel-scout-extended-fa72e41143b7

smknstd avatar Feb 26 '20 11:02 smknstd

Any update on this?

otherperspectives avatar Nov 01 '22 09:11 otherperspectives