Here we will see how to generate slug using str_slug in laravel. We will see Str::slug method in laravel. We will talk about laravel generate slug example. 

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 example below.

Given below are two examples:



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:

If we want to use helpers functions then we need to install new composer package with command as shown below:

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