PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   Laravel Unique Slug Generator   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Laravel Unique Slug Generator
Laravel service to generate URL slug text
Author: By
Last change:
Date: 11 months ago
Size: 3,272 bytes
 

Contents

Class file image Download

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.