Laravel str slug() helper function Example
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....
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial