In this tutorial you will learn about the Laravel str slug() helper function Example and its application with practical example.
In this article, I’ll show you how to generate seo friendly url slug using laravel Str::slug method. We will show example of laravel str_slug helper function in laravel. In this tutorial, generate seo friendly url slug with provided string using laravel Str::slug method.
Table Of Contents−
Laravel Generate URL Slug Using Str::slug method
The Str::is method is used to generate seo friendly URL “slug” with the provided string. Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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:-
1 |
example-of-str-slug |
SEO Friendly URL Slug Using str_slug helper function
You can also use the laravel Str::slug method as helper function called as str_slug(). When you want to use helpers functions then you need to install a composer package with following artisan command:
1 |
composer require laravel/helpers |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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:-
1 |
example-of-str-slug |