PHP code example of areia-lab / slug-uid

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

    

areia-lab / slug-uid example snippets


'slug' => [
    'separator' => '-',
    'max_length' => 150,
    'source_columns' => ['title','name'],
    'regen_on_update' => true,
]

'uid' => [
    'prefix' => 'UID',
    'length' => 16,
    'driver' => 'uuid4',
]

'sequence' => [
    'prefix' => 'ORD',
    'padding' => 5,
    'column' => 'post_sequence',
    'scoped'  => true,
    'separator' => '-',
]

// Generate slug
SlugUid::slug('Hello World');

// Unique slug for model
SlugUid::uniqueSlug(Post::class, 'Hello World');

// Generate UID
SlugUid::uid();
SlugUid::uniqueUid(Post::class, 'USR');

// Sequence
SlugUid::sequence(Post::class, 'ORD', 4);

SlugUid::slug('Hello World');
SlugUid::uniqueSlug($post);
SlugUid::uid('USR');
SlugUid::uniqueUid(Post::class, 'USR');
SlugUid::sequence(Post::class, 'INV');



namespace App\Models;

use AreiaLab\SlugUid\Traits\HasSequence;
use AreiaLab\SlugUid\Traits\HasSlug;
use AreiaLab\SlugUid\Traits\HasUid;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasSlug, HasUid, HasSequence;

    public $slug_column = 'slug';
    public $slug_source = 'title';

    public $uid_column = 'uid';
    public $uid_prefix = 'POST';

    public $sequence_column = 'post_sequence';
    public $sequence_prefix = 'PST';
    public $sequence_padding = 4;
    public $sequence_scoped = true;
    public $sequence_separator = '-';

    protected $fillable = ['title', 'slug', 'uid', 'post_sequence', 'description'];
}

return SlugUid::slug('Hello World');
// Output: hello-world

return SlugUid::uniqueSlug(Post::class, 'Hello World');
// Output: hello-world
// Output if slug exists: hello-world-1

return SlugUid::uid('prefix');
// Output: prefix-65e1d5ff5201a7

return SlugUid::uniqueUid(Post::class, 'prefix');
// Output: prefix-xxxxxxxxxxxxxx (unique)

return SlugUid::sequence(Post::class, 'PST');
// Output: PST-0001

return SlugUid::sequence(Post::class, 'INV', 4);
// Output: INV-0001

return Post::create([
    'title' => 'hello world',
    'slug' => SlugUid::uniqueSlug(Post::class, 'hello world'),
    'uid' => SlugUid::uniqueUid(Post::class, 'post'),
    'post_sequence' => SlugUid::sequence(Post::class),
    'description' => 'This is a test post.'
]);

return Post::create([
    'title' => 'hello world',
    'description' => 'This is a test post.'
]);

$post = Post::first();
$post->update([
    'title' => 'My First Post Updated',
    'description' => 'This is a test post desc.'
]);
return $post;
bash
php artisan vendor:publish --provider="AreiaLab\SlugUid\SlugUidServiceProvider" --tag=sluguid-config
bash
php artisan sluguid:regen App\Models\Post