我正在尝试使用 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/