Laravel str slug() helper function Example

10-Apr-2023

.

Admin

Hi Guys,

In this blog, I will show how to generate slug using str_slug in laravel. I want to learn you Str::slug method in laravel. we will talk about laravel generate slug example. This tutorial will give you how to generate slug using Str::slug.

The Str::slug method generates a URL friendly "slug" from the given string. If you want to generate slug in laravel then you can use bellow example. Here i will give you two example for generate slug in laravel So let's see bellow example.

Example 1


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$exampleSlug = Str::slug('Example of str slug');

dd($exampleSlug);

// Output

// example-of-str-slug

}

}

Output :

example-of-str-slug

Example 2

but you want to use helpers functions then you need to install new composer package with command as bellow:

composer require laravel/helpers

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class HomeController extends Controller

{

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$exampleSlug = str_slug('Example of str slug');

dd($exampleSlug);

// Output

// example-of-str-slug

}

}

Output :

example-of-str-slug

It will help you....

#Laravel 8

#Laravel 7

#Laravel

#Laravel 6