Coder Social home page Coder Social logo

Comments (5)

pixelreverb avatar pixelreverb commented on August 19, 2024 1

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.

bakerkretzmar avatar bakerkretzmar commented on August 19, 2024

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.

pixelreverb avatar pixelreverb commented on August 19, 2024

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.

pixelreverb avatar pixelreverb commented on August 19, 2024

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.

bakerkretzmar avatar bakerkretzmar commented on August 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.