-
-
Notifications
You must be signed in to change notification settings - Fork 60
[8.x] Allow custom relationship types #747
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
[8.x] Allow custom relationship types #747
Conversation
duncanmcclean
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
Although, can we use a hook instead of our own saveCustomRelationship method?
Ooh, yeah—good call on that! Tbh, I did not know you could use hooks like that, so let me know if this isn't what you had in mind. This updated approach of course requires different usage, something like this I think: Relationships::hook('saveCustomRelationship', function ($payload, $next) {
[
'relationship' => $relationship,
'values' => $values,
] = $payload;
if(get_class($relationship) === BelongsToJson::class){
$relationship->sync($values)->save();
}
return $next($payload);
}); |
|
That's perfect! I'll review this properly next week. 🙂 |
duncanmcclean
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good - thanks!
|
Released as part of v8.8.0. |
This PR adds functionality to the
Relationshipsclass, allowing for registration of custom save-methods. The core Runway features provide 95% of what's needed for custom relationships, but an error is thrown when using a third-party class since the only match cases are nativeHasManyandBelongsToManyeloquent relationships. We're just adding a default/fallback, to provide devs with an option for registering their ownsavecallback.A good example use-case would be the relationship classes found in the Eloquent JSON Relations package, where a belongs-to-many equivalent can exist in a JSON column. Here's how this could be used, from within a service provider:
And then the corresponding field from the resource's blueprint:
Obviously an unsolicited feature request, but would be a big improvement on super-dynamic projects that leverage JSON columns in multiple places. Please let me know if there's anything I can do to improve this (general updates, along with tests, doc updates, etc.) and I'll make it happen. Thanks for considering this!