laravel - 如何使用 Laravel Rinvex 属性?

标签 laravel laravel-5 attributes

我正在尝试使用 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'],
]);

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

但是当我尝试打电话时

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

我做错了什么?

最佳答案

您是否注册了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

关于laravel - 如何使用 Laravel Rinvex 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465091/

相关文章:

mysql - Laravel:MySQL 每天自动更新我的 Updated_at 字段,但文章没有任何更新

mysql - Eloquent 查询,可能的 Join

laravel - 如何在laravel数据表中添加行号或序列号

laravel - 针对一对多关系的工厂播种时,字段没有默认值

laravel - 开始升级 Laravel 应用程序后,自定义 Json Guard 不再起作用

javascript - 将包含重音符号的数组从 Laravel 传递到 vue.js

php - laravel 5.3护照和 Angular 存储访问 token

jquery - 如何使用 jQuery 更改输入字段中非 HTML 属性的值?

javascript - 确定 javascript 对象上的所有属性是否为 null 或空字符串

python - 设置多个类属性的简单方法?