PHP code example of maniruzzaman / laravel-unique-slug

1. Go to this page and download the library: Download maniruzzaman/laravel-unique-slug library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maniruzzaman / laravel-unique-slug example snippets


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

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

use Akash\LaravelUniqueSlug\Facades\UniqueSlug;

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

// 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

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

    return Category::create($data);
}

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


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,
];

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