Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Laravel:How to send different slug if already slug present

how-to-generate-unique-slug-in-laravel-8
$lower = strtolower($request->name);
$slug = str_replace(" ", "-", $lower);
$slug =str_replace(" ", "-", $slug);
$slug_get = Profile::where('slug', $slug)->get();
if(sizeof($slug_get) > 0){
$slug_count = count($slug_get)+1;
$slug = $slug.'-'.$slug_count;
}else{
$slug = $slug;

}

In Laravel, you can use the uniqid() function to generate a unique identifier. However, if you want to ensure uniqueness even when a slug already exists, you can modify the generated uniqid() value until you find a unique one. Here's an example of how you can achieve this:

use Illuminate\Support\Str;

...

$slug = 'existing-slug'; // Existing slug that you want to check against

$uniqueSlug = $slug; // Initialize the unique slug with the existing slug

while (Model::where('slug', $uniqueSlug)->exists()) {
    $uniqueSlug = $slug . '-' . Str::random(5); // Append a random string to the slug
}

// Now, $uniqueSlug will be a unique slug that doesn't exist in the database
Enter fullscreen mode Exit fullscreen mode

Image description

In the example above, Model represents your Laravel model class for the corresponding table. Replace it with the actual name of your model.

The code checks if the initial slug exists in the database. If it does, it appends a random string generated using Str::random(5) to the slug and checks again. This process continues until a unique slug is found.

You can adjust the length and complexity of the random string by changing the parameter passed to Str::random().

Remember to import the Str class at the top of your file:

use Illuminate\Support\Str;
Enter fullscreen mode Exit fullscreen mode

By using this approach, you can generate a unique identifier even when an existing slug is already present in the Laravel database.

In Laravel, you can use the uniqid() function to generate a unique identifier. Here's an example of how you can use uniqid() in Laravel:

$uniqueId = uniqid();
Enter fullscreen mode Exit fullscreen mode

This will generate a unique identifier based on the current timestamp. The generated unique ID will be a string consisting of alphanumeric characters.

You can customize the generated unique ID by providing a prefix and/or a more entropy parameter to the uniqid() function. Here's an example:

$prefix = 'user_';
$moreEntropy = true;

$uniqueId = uniqid($prefix, $moreEntropy);
Enter fullscreen mode Exit fullscreen mode

In this example, the $prefix variable is set to 'user_', which will be added to the beginning of the generated unique ID. The $moreEntropy variable is set to true, which increases the uniqueness of the identifier by including additional entropy.

Remember that uniqid() generates a unique ID based on the current timestamp, so if you call it multiple times within a short period, you might get similar IDs. If you need a more secure and globally unique identifier, consider using Str::uuid() or a similar UUID generation method provided by Laravel or other libraries.

use Illuminate\Support\Str;

$uuid = Str::uuid()->toString();
Enter fullscreen mode Exit fullscreen mode

The Str::uuid() method generates a version 4 UUID (Universally Unique Identifier), which is a 128-bit number represented as a string. The toString() method converts the UUID object to a string representation.

Another way

$item_title = $request->franchise_title;

         // generate item slug based on combination of uniq id and item_title slug
         $item_slug = str_slug($item_title);
         $item_slug_exist = Item::where('item_slug', $item_slug)->count();
         if($item_slug_exist > 0)
         {
             $item_slug = $item_slug . '-' . uniqid();
         }
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)