php - 如何在我的 Laravel 5 应用程序中使用 Illuminate\Support\Str::slug?

标签 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);
    });
}

当我在 tinker 中运行它时,出现以下错误:

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 文件中:

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

.. 不过似乎没有太大区别。

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

最佳答案

我认为你不需要在这里创建别名,所以只需添加

use Illuminate\Support\Str;

到你的模型。

关于php - 如何在我的 Laravel 5 应用程序中使用 Illuminate\Support\Str::slug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29281013/

相关文章:

php - 为什么 preg_replace 给出这个输出?

javascript - 关闭 jquery ui 模态后聚焦到元素

PHP PREG_REPLACE REGEX 提及用户名

javascript - 使用数据在 Javascript 中重定向 (laravel 5.3)

php - 比较两个查询 Laravel 的结果

php - mySQL问题如何知道每种颜色的个数

mysql - 如何在带有连接的复杂选择中获取由mysql中的公共(public)列合并的多行

php - CKEDITOR - 在 Laravel 5 中使用 AJAX 更新/创建产品失败

laravel-5 - 如何使用 laravel eloquent 连接 3 个表

php - Laravel 5.4 及以上版本 : Nothing posting to pivot table