Comments (4)
Hmmm ... I see what you're saying. If you create a new model and manually set the slug, then it shouldn't generate one for you. That makes logical sense to me, and could probably be solved by just reversing the order of the two if
statements in the needsSlugging
method.
It's probably a breaking change, though. I'd need to test it out against all the unit tests to be sure (and even then, it could still be breaking the behaviour that others have come to rely on).
An alternative would be to add a new check before the other two: if the model is new and the attribute has a value, then just leave it alone. Does that make sense to you?
from eloquent-sluggable.
I agree with all your thoughts. I believe that "fixing" this – no matter how you go about it – would always result in a breaking change, since changing existing behaviour is what this is all about.
As to your two first suggestions/thoughts:
1. Reordering the if statements
could probably be solved by just reversing the order of the two if statements in the needsSlugging method
This would affect the behaviour on updates as well. In other words: When updating a model and manually setting a slug, Sluggable won't generate a slug. While this makes logical sense to me, it does undermine your suggestion in the docs:
If you want to regenerate one or more of your model's slug fields, you can set those fields to null or an empty string before the update.
This would no longer work.
2. Don't generate slug if model is new and attribute has value
add a new check before the other two: if the model is new and the attribute has a value, then just leave it alone.
While this approach would avoid the issue of no longer being able to force slug generation on update, I am not a fan of it. In the name of fixing one inconsistency it would introduce another: Manually setting a slug would result in different behaviour when updating a model than when creating one.
My thoughts
I, personally, would go with your first suggestion (reordering if-statements). Yes, you'd lose the ability to force generating a new slug on update by setting the field to null, but I am not a fan of this "hack" anyways. I'd much rather make use of the SlugService::createSlug()
method and explicitly generate a new slug.
from eloquent-sluggable.
I thought about this a little more. Maybe this is of help?
Short:
protected function needsSlugging(string $attribute, array $config): bool
{
if ($value === null && $trim($value) === '') {
return true;
}
if ($this->model->isDirty($attribute)) {
return false;
}
if ($this->model->exists && $config['onUpdate'] === true) {
return true;
}
return false;
}
The same again, but with explanatory comments:
protected function needsSlugging(string $attribute, array $config): bool
{
// IF 1: Always generate a new slug if the current value is invalid
if ($value === null && $trim($value) === '') {
return true;
}
// IF 2: Never generate a new slug if a valid slug (see IF 1) has been set manually
if ($this->model->isDirty($attribute)) {
return false;
}
/*
* I believe that at this point only updates remain, since all new model creations
* either have a manually set slug value (triggered IF 2)
* or don't have any slug value at all (triggered IF 1).
*
* I believe that what remains are existing models with valid slug values in the database.
* However, I am not sure about that, which is why I still check for the models existence in the next if-condition.
*/
// IF 3: Generate a new slug if the current value is valid (see IF 1)
// and has not been set manually (see IF 2)
// and this is an update and a new slug should be generated on updates
if ($this->model->exists && $config['onUpdate'] === true) {
return true;
}
/*
* Now all that remains should be existing models with valid slug values in the database
* for which onUpdate is set to false.
*/
return false;
}
Please take all my input with a huge grain of salt. I am not an experienced developer at all and my understanding of PHP is very limited.
Edit: With this approach it would still be possible to force generating a new slug by setting the slug to null.
from eloquent-sluggable.
I am very sorry, I must have accidentally closed this issue :/ You can tell this is my first ever issue...
from eloquent-sluggable.
Related Issues (20)
- Conditionally force new slug HOT 4
- slug another language HOT 1
- Checking the pull request HOT 1
- Sluggable HOT 1
- throw new \UnexpectedValueException HOT 2
- Я
- Laravel 10 support HOT 8
- Laravel 10 Install issue... HOT 1
- Should findBySlugOrFail return Model instead? HOT 1
- Issue when testing a model factory using make() HOT 3
- Turkish character slug problem
- Is it possible to use the slug as source? HOT 4
- SluggableScopeHelpers Trait: Support for orWhere and whereIn HOT 1
- Slugging with Laravel Nova HOT 4
- Remove HTML Tags Before Creating Slug HOT 3
- Bug when using $with properties that include belongTo relationships HOT 7
- Case-sensitivity and slug uniqueness HOT 2
- Tag 11.0 version. Laravel 11 just landed 🌕 🚀 HOT 2
- Generate slug from the same field if explicitly modified HOT 2
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 eloquent-sluggable.