afzalsabbir / slug-generator
A simple package to generate slug from string for Laravel
v0.0.1-alpha-2
2022-09-17 16:30 UTC
Requires
- php: ^7.0|^8.0
- laravel/framework: ^5.6|^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- fzaninotto/faker: ^1.9@dev
- illuminate/support: ^5.8@dev
- mockery/mockery: ^1.0@dev
- orchestra/database: ^3.8@dev
- orchestra/testbench: ^3.8@dev
- phpunit/phpunit: ^7.4@dev
- symfony/thanks: ^1.0
This package is auto-updated.
Last update: 2024-11-17 21:15:39 UTC
README
A Laravel package to generate slugs for your models.
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 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();
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.