PHP code example of fahadchy24 / laravel-unique-slug

1. Go to this page and download the library: Download fahadchy24/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/ */

    

fahadchy24 / laravel-unique-slug example snippets


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

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

use Fahad\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, 'fahad', 'username', ''); // fahad

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

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

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 --provider="Fahad\LaravelUniqueSlug\UniqueSlugServiceProvider"