首页 > 解决方案 > 如何使用 Laravel Rinvex 属性?

问题描述

我正在尝试使用https://github.com/rinvex/attributes,但不明白如何。文档对我来说不清楚,我需要帮助。

安装软件包后,我接下来完成了:

我有我想要归属的 \App\Models\Product 类。所以我把模型

use Rinvex\Attributes\Traits\Attributable;

class Product extends Model
{
    use CrudTrait;
    use Sluggable;
    use Attributable;

/***/

在 AppServiceProvider 的 boot() 中:

app('rinvex.attributes.entities')->push(App\Models\Product::class);

接下来 - 就像在文档中一样 - 在 Tinker 控制台中创建属性,就像这样:

app('rinvex.attributes.attribute')->create([
    'slug' => 'size',
    'type' => 'varchar',
    'name' => 'Product Size',
    'entities' => ['App\Models\Product'],
]);

在数据库中,我看到属性条目和属性表中添加了条目。

但是当我尝试打电话时

$product->size = 50;
$product->save(); - got "not found field in Model".

我做错了什么?

标签: laravellaravel-5attributes

解决方案


你注册了varchar类型吗?正如它在文档中提到的, Rinvex 属性默认不注册任何类型(参见https://github.com/rinvex/laravel-attributes#register-your-types

所以我建议你在你的服务提供商注册方法中添加这个代码:

    Attribute::typeMap([
        'varchar' => \Rinvex\Attributes\Models\Type\Varchar::class,
    ]);

不要忘记包括这个类

use Rinvex\Attributes\Models\Attribute

推荐阅读