PHP code example of dewan / dewan-multilang-slug

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

    

dewan / dewan-multilang-slug example snippets


use App\Models\Team;

// First time create post with title Simple Post
MultilangSlug::makeSlug(Team::class, 'Team title');
// Output: team-title

// Second time create post with title Simple Post
MultilangSlug::makeSlug(Team::class, 'Team title');
// Output: team-title-1

// Third time create post with title Simple Post
MultilangSlug::makeSlug(Team::class, 'Team title');
// Output: team-title-2

use App\Models\Team;

// First time create post with title Simple Post
MultilangSlug::makeSlug(Team::class, 'Team title', 'team_slug');
// Output: team-title

// Second time create post with title Simple Post
MultilangSlug::makeSlug(Team::class, 'Team title', 'team_slug');
// Output: team-title-1

// Third time create post with title Simple Post
MultilangSlug::makeSlug(Team::class, 'Team title', 'team_slug');
// Output: team-title-2

public function store(array $data): Team|null
{
    $title = "here is title";
    $data = Team::create([
        "title" => $title,
        'slug' => MultilangSlug::makeSlug(Team::class, $title),
    ]);
    return $data;
}

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

    return Post::create($data);
}

MultilangSlug::makeSlug($model, $slugText, $field, $divider);

/**
 * Generate a Unique Slug.
 *
 * @param object $model
 * @param string $slugText
 * @param string $field
 * @param string $divider
 *
 * @return string
 * @throws \Exception
 */
public function makeSlug(
    $model,
    $slugText,
    $field,
    $divider = null
): string




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

   
    /*
    |--------------------------------------------------------------------------
    | Slug random text or number
    |--------------------------------------------------------------------------
    |
    | If slug already exist then by default slug will generated by random string like. 
    | test-zFU, test-W1U, test-0FR ....
    |
    | If false then slug will generated by number like
    | test-1, test-2, test-3 .... test-100
    |
    */
    'unique_slug' => true,

     /*
    |--------------------------------------------------------------------------
    | Slug random text limit
    |--------------------------------------------------------------------------
    |
    | Default 3 text key added in slug, the slug will generated like
    | test-zFU, test-W1U, test-0FR ....
    |
    */
    'random_text' => 3,
     /*
    |--------------------------------------------------------------------------
    | Slug max count limit
    |--------------------------------------------------------------------------
    |
    | Default 100, slug will generated like
    | test-1, test-2, test-3 .... test-100
    |
    */
    'max_count' => 100,
];
bash
php artisan vendor:publish --tag=dewan-multilang-slug-config --force