dewan/dewan-multilang-slug

It takes a string, a model, a filed, and a divider, and returns a unique string with a number or random string appended to it if the slug already exists in the database.

v3.0.2 2024-08-10 06:38 UTC

This package is auto-updated.

Last update: 2025-07-10 08:50:25 UTC


README

here is doc

Installation

Run this command for install package.

composer require dewan/dewan-multilang-slug

Run this command for config publish.

php artisan vendor:publish --tag=dewan-multilang-slug-config --force

Basic use

Example #01- Post unique slug from title

Let's assume, we have in Team class, Now, if we passed title and generate slug from that it will generated unique slug depend on slug field, then -

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

If we pass 3rd parameter and if we passed title and generate slug from that it will generated unique slug depend on team_slug field, then -

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

Example-1 - Unique slug for Team or any model easily

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

Example-2 - Unique slug for Post or any model easily

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

    return Post::create($data);
}

API Docs

Generate method -

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

Configurations

<?php

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

Contribution

You're open to create any Pull request.