How to create or generate slug in laravel 8

How to create slug in laravel 8 using slug function

In this post, you will be learning about how to generate a valid slug in laravel 8 using str_slug function.

So guys, lets see few example, that how we can make slug or generate slug in laravel.

Example 1: In your controller

use Illuminate\Support\Str;

$slug = Str::slug($request->slug);
return $slug;

Example 2: Generate Slug using str_slug() function.

$slug = str_slug($request->slug);
return $slug;

Exmaple 3: How to create a slug customly / manually in php

$string = preg_replace('/[^A-Za-z0-9\-]/', '-', $request->slug); //Removed all Special Character and replace with hyphen
$final_slug = preg_replace('/-+/', '-', $string); //Removed double hyphen
$slug = strtolower($final_slug);
return $slug;

Thank you.

© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT