How To Use str_slug() Helper Function In Laravel?

Dec 17, 2021 . Admin



Hi Dev,

This example is how to use str_slug() helper function in laravel?

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.

So let's start following 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
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
#Laravel