Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Illuminate\Support\Str::slug in my Laravel 5 app?

Tags:

I'm doing the following in the

public function boot(DispatcherContract $events) {     parent::boot($events);      // set Tag slug     Tag::saving(function($tag)     {         //slugify name         $tag->slug = Str::slug($tag->name);     }); } 

When I run it in tinker, I get the following error:

PHP Fatal error:  Class 'App\Providers\Str' not found in /var/www/questions-l5/app/Providers/EventServiceProvider.php on line 35 

..but I don't know the Laravel way to import it. Do I need to just use use, I tried to add the following to the config/app.php file:

'aliases' => [ ... 'Str'      => 'Illuminate\Support\Str', 

.. didn't seem to make much difference though.

http://chrishayes.ca/blog/code/laravel-4-generating-unique-slugs-elegantly http://laravel.com/api/5.0/Illuminate/Support/Str.html

like image 491
Martyn Avatar asked Mar 26 '15 14:03

Martyn


People also ask

What is str :: slug in laravel?

Laravel Str slug() function generates a URL friendly slug from the given string.27-Mar-2021.

What means in laravel?

Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern and based on Symfony.


1 Answers

I don't think you need to create alias here, so just add

use Illuminate\Support\Str; 

to your model.

like image 65
Limon Monte Avatar answered Oct 11 '22 08:10

Limon Monte