Comments (5)
Thanks for your reply. Your suggestion actually did the trick.
I think I was fooled by the fact the application is working because of the fallback to the id
property. I know this because the app contains other multi-word models (like RegistrationToken
) that - as I know now - are not properly declared and works just fine, including model binding.
from ziggy.
What is the specific error you're encountering here? Is there an exception being thrown anywhere or is the issue just that you don't see the binding in Ziggy's output? Do the routes themselves work as expected in your app?
This looks like an issue with the definition of the route or model, not with Ziggy, and I added a test for this particular setup locally and it passes (will PR it tomorrow). Can you share more of the real code where you're having this issue in your app, and/or a minimal reproduction? Thanks.
from ziggy.
I use a Laravel project together with React and InertiaJS.
The configuration of the project is pretty much as it is after a fresh install.
The specific problem I encounter is that the binding is missing, which according to the above mentioned definitions should be present.
I would expect the generate configuration to look something like:
'organizations.registration_tokens.show': {
uri: 'organizations/{organization}/registration_tokens/{registration_token}',
methods: ['GET', 'HEAD'],
parameters: ['organization', 'registration_token'],
bindings: { organization: 'slug', registration_token: 'token' }, // <- 'registration_token' is missing for me
},
// ...
'registration_tokens.show': {
uri: 'registration_tokens/{registration_token}',
methods: ['GET', 'HEAD'],
parameters: ['registration_token'],
bindings: { registration_token: 'token' }, // <- complete binding is missing for me
},
The routes themselves work when I would rely on the id
. There is no error or exception thrown.
I would like to access the routes in the form of
/organizations/{slug}/registration_tokens/{token}
which requires two bindings- and
/registration_tokens/{token}
which requires one binding.
When the ziggy configuration is used from React/InertiaJS side, it always resolves this url to
/organizations/{slug}/registration_token/{id}
- and
/registration_tokens/{id}
.
Which is completely understandable as id
is the fallback/default and the bindings are not generated.
Somehow the return value of getRouteKeyName
for the RegistrationToken
model is ignored.
What I found out is that I can get the binding to token
at least for the route GET /registration_tokens/{registration_token}
when I define the route as
Route::middleware(['verified', 'role:system_admin'])->group(function () {
Route::get('/registration_tokens/{registration_token:token}', [RegistrationTokenController::class, 'show'])->name('registration_tokens.show');
// ...
}
Which generates
'registration_tokens.show': {
uri: 'registration_tokens/{registration_token}',
methods: ['GET', 'HEAD'],
parameters: ['registration_token'],
bindings: { registration_token: 'token' },
},
But I assume that in this case the binding is taken from the route definition and not by evaluating getRouteKeyName
.
I really would like to continue using Route::resource(...)
and not need to define each route separately.
I hope this helps. I will checkout your test and setup a minimal working example on the weekend.
Thanks.
from ziggy.
As promised, you can have a look at a minimal working example here: mwe-ziggy-model-binding. It also contains my last set of generated routes at resources/js/ziggy.js
. Thanks.
from ziggy.
Thanks a lot for the reproduction! Here's a PR that fixes your issue: pixelreverb/mwe-ziggy-model-binding#1.
Your route parameters and controller method arguments must be exactly the same for route model binding to work, down to casing and spelling; your route parameter is registration_token
but your controller argument is $registrationToken
so they aren't recognized as being related to each other (that's how that Laravel feature works, it has nothing to do with Ziggy).
Update your organizations.registration_tokens
route definition to organizations.registrationTokens
(or update your RegistrationTokenController
method arguments to $registration_token
) and your example should work fine—but just for the nested resource.
Your registrationTokens
resource route needs its own separate controller, because route model binding also relies on the position of the route parameters and controller arguments, so those routes won't be bound correctly because the first controller method argument ($organization
) doesn't correspond to the first route parameter (registration_token
).
Let me know if any of that isn't clear!
from ziggy.
Related Issues (20)
- False negative with route().current() and nested params
- Using `route()->current('...*')` multiple time issue
- Arrow function given to Event::listen stops further propagating of the event HOT 2
- Vue Plugin isnt delivered with Composer package
- Laravel 11 support HOT 2
- Class "Tightenco\Ziggy\Ziggy" Not Found After Upgrading to Laravel 11 HOT 2
- This package does not work with Folio named routes HOT 1
- Passing Ziggy configuration to Inertia.js SSR in a Svelte app
- TypeError when optional params omitted from array
- package does not handle a Vue 2 codebase well HOT 1
- Vue: Cannot convert undefined or null to object
- Feature: Add ```route().queryParams``` to get only query params without laravel route params
- Export Helper type to get the parameters type of specific route
- Missing alert or Logic problem on current route method
- Ziggy generates URL with a double leading slash HOT 6
- Error message : Ziggy is not defined
- Add an option for strict route checking HOT 2
- "Resolving Infinite Loop Issues in resolveBindings Function"
- Ziggy generating wrong params for resources with param url HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ziggy.