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

后端 未结 6 921
孤独总比滥情好
孤独总比滥情好 2021-01-03 22:46

I\'m doing the following in the

public function boot(DispatcherContract $events)
{
    parent::boot($events);

    // set Tag slug
    Tag::saving(function(         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 23:38

    I had the exact same issue. Adding the line to aliases in the app\config\app.php file is the correct way to fix this.

    'alias' => [
         ...
         'Str' => Illuminate\Support\Str::class,
         ...
    ],
    

    Doing it this way avoids:

    1. the need to add a use Illuminate\Support\Str; line in your blade file
    2. the need to fully quality the class, i.e \Illuminate\Support\Str::slug($tag->name)

    In fact you can see that Mr. Otwell includes this line in Laravel 5.8.

    I'm not sure why this didn't work for you. It could be that your Laravel is out of date. Make sure you've upgraded to 5.8 and then run composer update.

提交回复
热议问题