You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’d like to propose a new feature called HasSlug that automatically generates a slug from a specific field (e.g., title) and stores it in another field (e.g., slug). This trait could be useful in many projects that require slug generation (e.g., blogs, content management systems, e-commerce platforms, etc.), helping eliminate repetitive code for slug creation in observers or controllers.
Why is This Feature Needed?
In Laravel projects, we often need to generate slugs from fields like title or name to use in URLs. Typically, developers create Observers or use external packages like spatie/laravel-sluggable or cviebrock/eloquent-sluggable to handle this task.
However, for projects that don’t require advanced features, we still need a simple, lightweight, and dependency-free solution.
Benefits of HasSlug:
Simple and Lightweight: Just one trait to generate slugs.
No External Dependencies: No need for additional packages.
Customizable Fields: Easily change the source and destination fields.
Example Usage:
use Illuminate\Database\Eloquent\Model;
use Rayiumir\HasSlug\HasSlug;
class Post extends Model
{
use HasSlug;
}
If you want to use a custom field for slug generation, you can easily do that:
class Post extends Model
{
use HasSlug;
protected $slugSourceField = 'title';
protected $slugDestinationField = 'url_slug';
}
Why I Created This Trait:
Packages like spatie/laravel-sluggable or cviebrock/eloquent-sluggable are great, but for many smaller projects, these packages can feel too complex and heavy. I realized that having a simple, lightweight trait could make things easier for developers and reduce the need for extra packages.
This trait also allows developers to easily use slug generation without the need for repetitive code or complex configuration.
Conclusion:
Since slug generation is such a common need in many applications, I believe it would be beneficial to add this trait to the core Laravel framework. As a simple and useful feature, it could help reduce the overhead for developers and make Laravel even more attractive for smaller projects.
I’d love to hear your thoughts on this and would be happy to open a Pull Request if this sounds like a feature worth including in the core.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello Laravel team! 👋
I’d like to propose a new feature called HasSlug that automatically generates a slug from a specific field (e.g., title) and stores it in another field (e.g., slug). This trait could be useful in many projects that require slug generation (e.g., blogs, content management systems, e-commerce platforms, etc.), helping eliminate repetitive code for slug creation in observers or controllers.
Why is This Feature Needed?
In Laravel projects, we often need to generate slugs from fields like title or name to use in URLs. Typically, developers create Observers or use external packages like spatie/laravel-sluggable or cviebrock/eloquent-sluggable to handle this task.
However, for projects that don’t require advanced features, we still need a simple, lightweight, and dependency-free solution.
Benefits of HasSlug:
Simple and Lightweight: Just one trait to generate slugs.
No External Dependencies: No need for additional packages.
Customizable Fields: Easily change the source and destination fields.
Example Usage:
If you want to use a custom field for slug generation, you can easily do that:
Why I Created This Trait:
Packages like spatie/laravel-sluggable or cviebrock/eloquent-sluggable are great, but for many smaller projects, these packages can feel too complex and heavy. I realized that having a simple, lightweight trait could make things easier for developers and reduce the need for extra packages.
This trait also allows developers to easily use slug generation without the need for repetitive code or complex configuration.
Conclusion:
Since slug generation is such a common need in many applications, I believe it would be beneficial to add this trait to the core Laravel framework. As a simple and useful feature, it could help reduce the overhead for developers and make Laravel even more attractive for smaller projects.
I’d love to hear your thoughts on this and would be happy to open a Pull Request if this sounds like a feature worth including in the core.
Thanks and best regards!
Beta Was this translation helpful? Give feedback.
All reactions