A Laravel package to generate slugs for your models.
You can install the package via composer:
composer require afzalsabbir/slug-generator
- Using this package is very simple. Just use the
SlugGenerator
trait in your model and it'll automatically generate a slug for you. It'll use default configuration.// ... use AfzalSabbir\SlugGenerator\SlugGenerator; use Illuminate\Database\Eloquent\Model; // ... class Post extends Model { // ... use SlugGenerator; // ... }
- Publish the config file to customize the slug generation.
[Note: if facing any issue runphp artisan vendor:publish
and findAfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider
to publish]php artisan vendor:publish --provider="AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider" --tag="config"
- You can also configure the slug generation in your model.
namespace App\Models; use AfzalSabbir\SlugGenerator\SlugGenerator; use Illuminate\Database\Eloquent\Model; // ... class Post extends Model { // ... use SlugGenerator; // ... protected $slugGenerator = [ "set-on-create" => true, // Whether to set the slug when the model is created "set-on-update" => false, // Whether to update the slug when the target field is updated "target-field" => "title", // The field that will be used to generate the slug "separator" => "-", // The separator that will be used to separate the words "slug-field" => "slug", // The field that will be used to store the slug ]; // ... }
- You can also use the
generateSlug(Model $model)
helper method to generate a slug.use AfzalSabbir\SlugGenerator\SlugGenerator; use App\Models\Post; // ... $post = Post::find(1); $post->title = "Hello World"; $post->slug = generateSlug($post); $post->save();
Run the tests with:
vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email afzalbd1@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.