Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.1] WIP: Changes to implement custom pivot model events on attach & detach. #10545

Closed
wants to merge 2 commits into from
Closed

[5.1] WIP: Changes to implement custom pivot model events on attach & detach. #10545

wants to merge 2 commits into from

Conversation

olsgreen
Copy link
Contributor

Hey,

Implemented fully backwards compatible changes to properly fire events for custom pivot model attachment & detachment.

Also implemented a more fluent way of setting custom pivot models in their relation accessors as such:

public function relation()
{
    return $this->belongsToMany(\My\Name\Relation::class)
        ->setPivotModel(\My\Name\CustomPivotModel::class);
}

Thoughts?

@GrahamCampbell
Copy link
Member

You know you could have just download the diff from StyleCI and applied it using git apply rather than trying to fix it manually and still not getting it right.

@olsgreen
Copy link
Contributor Author

@GrahamCampbell missed the option to download the diff from StyleCI, thanks for pointing that out. Rushed manual fix commit from departure lounge at Gatwick at 4am, I should have known better! Any comments on the proposal itself?

@@ -90,6 +97,28 @@ public function __construct(Builder $query, Model $parent, $table, $foreignKey,
}

/**
* Set the custom pivot model class.
*
* @param string $class
Copy link
Member

Choose a reason for hiding this comment

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

Are you not missing a return type here?

@taylorotwell
Copy link
Member

Going to hold off on this. You can basically do this yourself by writing a method on your model. Say you have a user that has roles. Instead of using $user->roles()->attach()... write a $user->addRole($role) method that does what you want and fires the custom events you want for your application.

@samwigginshal
Copy link

Why not fire events for pivot attach and detach methods? Seems like a perfectly logical thing to do. Events are fired on every model persistence method after all. Also, @taylorotwell your solution it's fine if used before an app has already been created but retrospectively would be a pain to refactor

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