laravel - Laravel - 是否可以通过数组元素作为表格中的文本在模型(belongsTo)之间建立关系
问题描述
我是 Laravel 的绝对初学者。
是否有可能通过数组元素在模型之间建立关系,比如 belongsTo。
table1 中的列如下所示:
mapping: ["misc","rates","photos"]
我需要类似于以下内容:
public function dependencies() : BelongsTo
{
// This is impossible because "mapping" is array in Depends model and text type in database.
return $this->belongsTo(Depends::class, 'mapping', 'element_of_array');
}
或类似的东西(hasMany 等)。
表如下所示:
CREATE TABLE `dependings` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`media_id` int(10) unsigned NOT NULL,
`type` varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL,
`mapping` text COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `dependent_collection_mappings_collection_id_type_unique` (`media_id`,`type`),
CONSTRAINT `dependent_collection_mappings_collection_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
解决方案
推荐阅读
- swift - 定义状态变量后以编程方式推送视图
- python - 熊猫中的“放大设置”行为
- javascript - 如何使用 React 高阶函数最大化可组合性?
- javascript - 如何在 javascript 中编写公式并使用 html 显示公式并显示结果?
- python - 如何找到形成零和的子组?
- python - 如何在 Collaboratory 中将 tsv 读入 int np 数组
- mongodb - 每 x 秒/分钟发送和接收重复信息的架构
- c# - 使用httpclient C#模拟两次点击并通过PKI证书
- omnet++ - 如何在 OMNeT++ IDE 中禁用引号和括号的自动完成
- angular - 使用 Angular 项从字符串创建动态 CSS 样式