laravel - Laravel belongsTo 与演员不工作
问题描述
我有模型应用程序和模型属性。
在表应用程序中,我有列:属性(类型 json) 在这些列中,我有属性的 id。例子:
["5", "6", "7"]
此数据在表应用列中attributes
。
在模型应用程序中,我写了关系:
protected $casts = [
'attributes' => 'array',
];
public function attribute() {
return $this->belongsTo('App\Attribute', 'attributes');
}
但我只得到 id 的属性5
。但需要获得所有属性。为什么我只得到一个属性?我试过了,hasMany
我得到了同样的结果。
解决方案
我创建了一个带有 JSON 关系的包:https ://github.com/staudenmeir/eloquent-json-relations
您可以像这样创建多对多关系:
class Application extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
protected $casts = [
'attributes' => 'array'
];
public function attribute()
{
return $this->belongsToJson(Attribute::class, 'attributes');
}
}
class Attribute extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
public function applications()
{
return $this->hasManyJson(Application::class, 'attributes');
}
}
推荐阅读
- angular - 如何在 ngFor Loop 中动态设置 Kendo 组合框或 Kendo 下拉列表的值?
- php - 当我单击注册按钮时,laravel 7没有任何反应,似乎事件没有触发
- amazon-web-services - 如何使用 aws-cdk 获取托管在 VPC 中的 SFTP 服务器的子网 IP 地址?
- python - 试图了解如何解释 LSTM 神经网络的“准确度分数”输出
- php - 带有 Neos CMS 的 AWS Lambda 的 bref.sh php 容器
- java - 如何创建 SSL 连接客户端类以连接到现有服务器?
- swiftui - 如何在 swiftUI 中将文本旁边的颜色添加到列表中?(数据流)
- r - 将时间四舍五入到特定时间,即使是前一天
- c - C语言阶乘函数
- c++ - 如何按项目索引返回与值索引相对的枚举?