Mar*_*tyn 16 php laravel laravel-5
我正在做以下事情
public function boot(DispatcherContract $events)
{
parent::boot($events);
// set Tag slug
Tag::saving(function($tag)
{
//slugify name
$tag->slug = Str::slug($tag->name);
});
}
Run Code Online (Sandbox Code Playgroud)
当我在修补程序中运行它时,我收到以下错误:
PHP Fatal error: Class 'App\Providers\Str' not found in /var/www/questions-l5/app/Providers/EventServiceProvider.php on line 35
Run Code Online (Sandbox Code Playgroud)
..但我不知道Laravel导入它的方式.我是否需要使用use
,我尝试将以下内容添加到config/app.php文件中:
'aliases' => [
...
'Str' => 'Illuminate\Support\Str',
Run Code Online (Sandbox Code Playgroud)
..但似乎没有太大的区别.
http://chrishayes.ca/blog/code/laravel-4-generating-unique-slugs-elegantly http://laravel.com/api/5.0/Illuminate/Support/Str.html
Lim*_*nte 37
我不认为你需要在这里创建别名,所以只需添加
use Illuminate\Support\Str;
Run Code Online (Sandbox Code Playgroud)
到你的模型.
ste*_*fel 10
在Laravel 5中,您可以str_slug($text)
直接使用.您不再需要使用Facade.
http://laravel.com/docs/5.1/helpers#method-str-slug
我有完全相同的问题。将行添加到文件中的别名app\config\app.php
是解决此问题的正确方法。
'alias' => [
...
'Str' => Illuminate\Support\Str::class,
...
],
Run Code Online (Sandbox Code Playgroud)
这样做可以避免:
use Illuminate\Support\Str;
在刀片文件中添加一行\Illuminate\Support\Str::slug($tag->name)
事实上你可以看到 Otwell 先生在Laravel 5.8中包含了这一行。
我不确定为什么这对你不起作用。可能是你的 Laravel 已经过时了。确保您已升级到 5.8,然后运行composer update
.