如何在Laravel 5应用程序中使用Illuminate \ Support \ Str :: slug? [英] How to use Illuminate\Support\Str::slug in my Laravel 5 app?

查看:602
本文介绍了如何在Laravel 5应用程序中使用Illuminate \ Support \ Str :: slug?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在

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

..但是我不知道Laravel导入它的方式.我是否只需要使用use,我尝试将以下内容添加到config/app.php文件中:

..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

推荐答案

我认为您无需在此处创建别名,所以只需添加

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

use Illuminate\Support\Str;

到您的模型.

这篇关于如何在Laravel 5应用程序中使用Illuminate \ Support \ Str :: slug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆