Skip to content

[7.x] use getTouchedRelations when touching owners#34100

Merged
taylorotwell merged 1 commit into
laravel:7.xfrom
patrickomeara:7.x
Sep 3, 2020
Merged

[7.x] use getTouchedRelations when touching owners#34100
taylorotwell merged 1 commit into
laravel:7.xfrom
patrickomeara:7.x

Conversation

@patrickomeara
Copy link
Copy Markdown
Contributor

This allows overriding getTouchedRelations()

I want to update certain polymorphic parents.

Using the example from the documentation: if a comment is updated it should update post, but never video

class Comment extends Model
{
    protected $touches = ['commentable'];
    /**
     * Get the owning commentable model.
     */
    public function commentable()
    {
        return $this->morphTo();
    }

    public function getTouchedRelations()
    {
        return $this->commentable_type === 'video' ? [] : $this->touches;
    }
}

getTouchedRelations() isn't currently used in the framework anywhere, overriding it has no affect, it's simply a getter.

The alternative solution to my problem is to use setTouchedRelations() when the model is booted. However, overriding most get*() methods in the framework actually have an affect, whereas this one doesn't.

This is backwards compatible as the base getTouchedRelations() on HasRelationships simply returns $this->touches.

This allows overriding getTouchedRelations
@driesvints driesvints changed the title use getTouchedRelations when touching owners [7.x] use getTouchedRelations when touching owners Sep 3, 2020
@taylorotwell taylorotwell merged commit 0933309 into laravel:7.x Sep 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants