Coder Social home page Coder Social logo

slug-generator's Introduction

SlugGenerator - Laravel Slug Generator

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 [email protected] instead of using the issue tracker.

License

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

slug-generator's People

Contributors

afzalsabbir avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.