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

Adds support for implicit route model binding with translated slugs #213

Merged
merged 24 commits into from
Dec 15, 2021

Conversation

marvin-wtt
Copy link
Contributor

Adds implicit route model binding support for translated slugs using spatie/laravel-translatable if the route key matches the slug field

@freekmurze
Copy link
Member

Seems interesting, but currently tests and updated readme seem to be missing. Could you take care of that?

@marvin-wtt
Copy link
Contributor Author

marvin-wtt commented Nov 11, 2021

@freekmurze Thanks for the reply. I updated the readme and added a brief explanation. Unfortunately I was unable to add test cases because sqlite doesn't support json without the JSON1 extention. I added a try-catch block around the added operation to avoid conflicts with databases that don't support json.
Do you want me to squash the commits?

@freekmurze
Copy link
Member

In that case, we'll need to switch to using MySQL for the tests. Currently I don't have the time to take care of this, but I accept a separate PR that does this.

@marvin-wtt
Copy link
Contributor Author

Turns out I didn't read the laravel docs about JSON Where Clauses properly. Fortunately SQLite does support json but not Laravels whereJsonContains method. I updated it to use the the JSON_EXTRACT() function instead and it works for testing.

src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
tests/HasTranslatableSlugTest.php Outdated Show resolved Hide resolved
src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
tests/HasTranslatableSlugTest.php Show resolved Hide resolved
tests/TestCase.php Outdated Show resolved Hide resolved
tests/TranslatableModelSoftDeletes.php Outdated Show resolved Hide resolved
@freekmurze
Copy link
Member

Let me know when this is ready for review.

@marvin-wtt
Copy link
Contributor Author

I added some questions to your change request comments where I was unsure what to do. Except that, it's ready for review.

src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
tests/TestCase.php Outdated Show resolved Hide resolved
tests/TranslatableModelSoftDeletes.php Outdated Show resolved Hide resolved
Copy link
Member

@freekmurze freekmurze left a comment

Choose a reason for hiding this comment

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

It's going in the right direction 👍

README.md Show resolved Hide resolved
src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
src/HasTranslatableSlug.php Outdated Show resolved Hide resolved
tests/HasTranslatableSlugTest.php Show resolved Hide resolved
tests/TestCase.php Outdated Show resolved Hide resolved
@marvin-wtt
Copy link
Contributor Author

I just realized that my comments mentioned above where still pending 🙈. I'll take care of the change request this evening

@marvin-wtt
Copy link
Contributor Author

marvin-wtt commented Dec 8, 2021

@freekmurze As you mentioned in the review comment, child model resolution was not working correctly as it is only handled by the parent model. I created a PR which fixes this problem: laravel/framework#39929 . This however means that implicit route model binding only works from the next release (probably v8.76.0+).
I could also keep the current changes to at least support implicit route model binding for the parent model for lower version - this would be unnecessary for all future versions though.
How would you prefer to proceed here?

@freekmurze
Copy link
Member

As you mentioned in the review comment, child model resolution was not working correctly as it is only handled by the parent model. I created a PR which fixes this problem: laravel/framework#39929 . This however means that implicit route model binding only works from the next release (probably v8.76.0+).

Instead of checking for the specific Laravel version number (and adding a note), feel free to update the required Laravel version in composer.json 👍

Seems like the tests are failing, could you take a look at that?

@marvin-wtt
Copy link
Contributor Author

Instead of checking for the specific Laravel version number (and adding a note), feel free to update the required Laravel version in composer.json 👍

I can do that for laravel framework v8 but not for v7.

Seems like the tests are failing, could you take a look at that?

The test fails because the version I check on should be replaced with the next release. I tested with 8.x-dev and all tests passed. I'll fix that as soon as the new framework version is released

@freekmurze
Copy link
Member

I can do that for laravel framework v8 but not for v7.

Let's just drop support for Laravel 7.

I'll fix that as soon as the new framework version is released

Great, thanks! Feel free to poke me again then.

@marvin-wtt
Copy link
Contributor Author

marvin-wtt commented Dec 14, 2021

I updated the framework version to v8.76.1

@freekmurze Do you think it would be useful to not only query for the current locale but for all enabled locales to better support url sharing? Currently url sharing is only possible within the same locale.
To accomplish that it would be better to store the translated slugs in a separate table to make the query more effizient imo.

@freekmurze
Copy link
Member

Do you think it would be useful to not only query for the current locale but for all enabled locales to better support url sharing?

I think just enable this for the current locale is fine 👍

Thanks for your work on this PR.

@freekmurze freekmurze merged commit 6eabac0 into spatie:main Dec 15, 2021
@marvin-wtt marvin-wtt deleted the implicit-model-binding branch December 15, 2021 10:33
@marvin-wtt
Copy link
Contributor Author

@freekmurze First of all thanks for merging

I think just enable this for the current locale is fine 👍

I need this for a project anyway. The question is if you'd like to have this as a feature in this project and what your opinion on a separate table to improve querying is.

My idea would be like this:

id -> bigint
model -> string
model_id -> bigint
locale -> string
slug -> string

This could be a PR for laravel-translatable as well but I think it is intended to keep it as json for simplicity.

Alternatively I could also image to query the model like this:

foreach ($locales as $locale) {
    $query->orWhere("{$field}->{$locale}", $value);
}

But afaIk, it is bad practice to query with json like this

@@ -17,7 +17,7 @@
],
"require": {
"php": "^8.0",
"illuminate/database": "^8.0",
"illuminate/database": "^8.76.1",
Copy link

Choose a reason for hiding this comment

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

sail@f34348e1ecf9:/var/www/html$ composer require spatie/laravel-sluggable
Using version ^3.2 for spatie/laravel-sluggable
./composer.json has been updated
Running composer update spatie/laravel-sluggable
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires spatie/laravel-sluggable ^3.2 -> satisfiable by spatie/laravel-sluggable[3.2.0].
    - spatie/laravel-sluggable 3.2.0 requires illuminate/database ^8.76.1 -> found illuminate/database[v8.76.1, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.


Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What other packages do you use? And which version do you use for your project?

Copy link

Choose a reason for hiding this comment

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

image
I was able to install only after updating all packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants