laravel - 如何使用 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".
我做错了什么?
解决方案
你注册了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
推荐阅读
- vuejs2 - vue-router, webpack-dev-server 和 historyApiFallback, /foo 没问题但是 /foo/:bar 给出 404 (/foo/bundle.js)
- .net - 对托管在 IIS 上的 FileNet .Net 客户端的 Kerberos 支持
- android - 应用程序的名称是否受法律条款约束,还是仅使用公司名称?
- windows - 如何通过命令行从 Windows 8.1 任务栏中删除语言指示器?
- post - 使用 $_POST 从多个括号中获取值
- ruby-on-rails - Rails 在控制器中扩展模块
- javascript - 无法从 express js 使用 mongodb 登录
- javascript - 操作与表单提交之间的冲突
- c# - 带有 Microsoft OCR 的 Visual Studio
- python - 通过终止或桌面打开的pycharm具有不同的安装包,在同一个解释器中配置