Giter Site home page Giter Site logo

Comments (7)

slashequip avatar slashequip commented on May 14, 2024 1

@brendt I'll see if I can find some time this week to test my initial thoughts and I'll ping you a PR.

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

Hey and thanks for reporting this issue!

This is kind of a difficult one to tackle, because if we're not overriding getRouteKeyName, you'll get binary encoded values in generated URLs if I'm not mistaken.

The encoding and decoding needs to happen somewhere, and I'm not a 100% sure where the best place for it should be. A middleware seems like the correct place, but you would still need to add it to your routes using UUIDs in URLs.

Do you see other solutions which might solve your case in a better way?

from laravel-binary-uuid.

DennisOng avatar DennisOng commented on May 14, 2024

Hey Brendt, I'm not too familiar with the 'url generation' part that you mentioned, do you mean when doing something like
url("/posts/{$post->id}")?

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

In that case you're manually generating URLs, but if you're using route model binding, Laravel will automagically resolve things for you. Without overriding getRouteKeyName, the binary ID will be used, which we don't want.

from laravel-binary-uuid.

slashequip avatar slashequip commented on May 14, 2024

I was just looking into this issue myself for my own project.

I was tempted to make a tweaked version of the Illuminate\Routing\Middleware\SubstituteBindings middleware & related classes which will can look up the model by the binary UUID - this would potentially cause issues with non-uuid models.

Alternatively and probably much easier would be to overwrite the resolveRouteBinding function from the Modelclass:

/**
 * Retrieve the model for a bound value.
 *
 * @param  mixed  $value
 * @return \Illuminate\Database\Eloquent\Model|null
 */
 public function resolveRouteBinding($value)
 {
     // return $this->where($this->getRouteKeyName(), $value)->first();
     return $this->where($this->getRouteKeyName(), self::encodeUuid ($value))->first();
 }

This overwritten method could be included in the HasBinaryUuid trait quite easily. Not tested the above yet but I figured I'd share my thoughts.

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

@slashequip If you're up for it, feel free to send a PR, we might indeed merge this instead of overriding getRouteKeyName.

There's also talk to add binary UUIDs and binding support in Laravel itself, though it's nowhere near concrete yet. That would of course be the best solution.

from laravel-binary-uuid.

brendt avatar brendt commented on May 14, 2024

#36 was merged and tagged: https://github.com/spatie/laravel-binary-uuid/releases/tag/1.1.5

from laravel-binary-uuid.

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.