afzalsabbir/slug-generator

A simple package to generate slug from string for Laravel

v0.0.1-alpha-2 2022-09-17 16:30 UTC

This package is auto-updated.

Last update: 2024-04-17 19:59:14 UTC


README

A Laravel package to generate slugs for your models.

Software License Build Status Quality Score

Latest Stable Version Latest Unstable Version Total Downloads

Installation

You can install the package via composer:

composer require afzalsabbir/slug-generator

Usage

  • 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 run php artisan vendor:publish and find AfzalSabbir\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();

Testing

Run the tests with:

vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email afzalbd1@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.