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

Add support for custom scoped route model bindings #307

Merged
merged 8 commits into from
Jun 30, 2020

Conversation

bakerkretzmar
Copy link
Collaborator

This PR adds support for custom scopes in route model bindings.

Laravel 7 added the ability to scope nested route model bindings on a specific attribute:

// This route will look for a Post using the 'slug' attribute
Route::get('users/{user}/posts/{post:slug}', function (User $user, Post $post) {
    return $post;
})->name('users.posts');

Ziggy already supports passing an object parameter and automatically detecting an id key if one is present:

route('users.posts', [{ id: 4, name: 'Jacob' }, 'ziggy-v1-announcement'])
// Returns http://example.com/users/4/posts/ziggy-v1-announcement

This PR adds support for passing an object as a parameter with a custom binding scope:

route('users.posts', [{ id: 4, name: 'Jacob' }, { published: false, slug: 'ziggy-v1-announcement' }])
// Now also returns http://example.com/users/4/posts/ziggy-v1-announcement

Similarly to the existing behaviour with id, this only works if there are no conflicting route parameter names, so the example above won't necessarily work if there is another parameter explicitly called just slug.

@bakerkretzmar bakerkretzmar added this to the v1.0 milestone Jun 26, 2020
Copy link
Collaborator

@jakebathman jakebathman left a comment

Choose a reason for hiding this comment

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

Good stuff, LGTM 👍

tests/Unit/CommandRouteGeneratorTest.php Show resolved Hide resolved
@bakerkretzmar bakerkretzmar merged commit efab169 into develop Jun 30, 2020
@bakerkretzmar bakerkretzmar deleted the jbk/route-model-binding branch June 30, 2020 02:14
bakerkretzmar added a commit that referenced this pull request Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants