Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[5.0] Fix touchOwners() on many to many relationship.#8360

Merged
taylorotwell merged 3 commits into
laravel:5.0from
SebastianBerc:touchOwners-on-many-to-many
Apr 12, 2015
Merged

[5.0] Fix touchOwners() on many to many relationship.#8360
taylorotwell merged 3 commits into
laravel:5.0from
SebastianBerc:touchOwners-on-many-to-many

Conversation

@SebastianBerc
Copy link
Copy Markdown
Contributor

If we assign many to many relation to $touches property, we will get an exception on every call for touchOwners(): Call to undefined method Illuminate\Database\Eloquent\Collection::touchOwners().
So I iterate on each model within collection to call touchOwners() method on it instead of collection instance.

@JoostK
Copy link
Copy Markdown
Contributor

JoostK commented Apr 9, 2015

Considering Illuminate\Database\Eloquent\Collection is part of Eloquent, we could just implement touchOwners on the collection and let it delegate the call to all models?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now this isnull code is running twice

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given instanceof checks, is_null is redundant. This will do:

foreach ($this->touches as $relation)
{
    $this->$relation()->touch();

    if ($this->$relation instanceof Model)
    {
        $this->$relation->touchOwners();
    }
    elseif ($this->$relation instanceof Collection)
    {
        $this->$relation->each(function (Model $related)
        {
            $related->touch();
        });
    }
}

And btw I agree that it belongs to the Eloquent\Collection, but as far as I know @taylorotwell didn't want it there?

@GrahamCampbell GrahamCampbell changed the title Fix touchOwners() on many to many relationship. [5.0] Fix touchOwners() on many to many relationship. Apr 11, 2015
taylorotwell added a commit that referenced this pull request Apr 12, 2015
[5.0] Fix touchOwners() on many to many relationship.
@taylorotwell taylorotwell merged commit 5ce1f64 into laravel:5.0 Apr 12, 2015
flyingL123 added a commit to flyingL123/framework that referenced this pull request Nov 20, 2017
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.

5 participants