Git Product home page Git Product logo

laravel-unique-slug-generator's Introduction

Laravel-unique-slug-generator

A simple but beautiful unique slug generator for Laravel eloquent model.


Installation

composer require maniruzzaman/laravel-unique-slug

Configuration

Service Provider Registration In config/app.php, add in providers array -

'providers' => [
    // ...
    Akash\LaravelUniqueSlug\UniqueSlugServiceProvider::class,
    // ...
],

Facade Class Alias Add in aliases array -

'aliases' => Facade::defaultAliases()->merge([
    // ...
    'UniqueSlug' => Akash\LaravelUniqueSlug\Facades\UniqueSlug::class,
    // ...
])->toArray(),

Use from Controller

Import first the UniqueSlug facade

use Akash\LaravelUniqueSlug\Facades\UniqueSlug;

Example #01- Post unique slug from title

Let's assume, we have in Post class, we've added slug column which is unique. Now, if we passed title and generate slug from that, then -

use App\Models\Post;

// First time create post with title Simple Post
UniqueSlug::generate(Post::class, 'Simple Post', 'slug');
// Output: simple-post

// Second time create post with title Simple Post
UniqueSlug::generate(Post::class, 'Simple Post', 'slug');
// Output: simple-post-1

// Third time create post with title Simple Post
UniqueSlug::generate(Post::class, 'Simple Post', 'slug');
// Output: simple-post-2

Example #02 - Pass custom separator

Let's assume separator is '' empty.

// First time create user.
UniqueSlug::generate(User::class, 'akash', 'username', ''); // akash

// Second time create user.
UniqueSlug::generate(User::class, 'akash', 'username', ''); // akash1

// Third time create user.
UniqueSlug::generate(User::class, 'akash', 'username', ''); // akash2

Example - Unique slug for category or any model easily

public function create(array $data): Category|null
{
    if (empty($data['slug'])) {
        $data['slug'] = UniqueSlug::generate(Category::class, $data['name'], 'slug');
    }

    return Category::create($data);
}

API Docs

Generate method -

UniqueSlug::generate($model, $value, $field, $separator);
/**
 * Generate a Unique Slug.
 *
 * @param object $model
 * @param string $value
 * @param string $field
 * @param string $separator
 *
 * @return string
 * @throws \Exception
 */
public function generate(
    $model,
    $value,
    $field,
    $separator = null
): string

Publish configuration

php artisan vendor:publish akash/laravel-unique-slug

Configurations

return [
    /*
    |--------------------------------------------------------------------------
    | Slug default separator.
    |--------------------------------------------------------------------------
    |
    | If no separator is passed, then this default separator will be used as slug.
    |
    */
    'separator' => '-',

    /*
    |--------------------------------------------------------------------------
    | Slug max count limit
    |--------------------------------------------------------------------------
    |
    | Default 100, slug will generated like
    | test-1, test-2, test-3 .... test-100
    |
    */
    'max_count' => 100,
];

Contribution

You're open to create any Pull request.

laravel-unique-slug-generator's People

Contributors

maniruzzamanakash 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.