【发布时间】:2017-05-19 10:15:58
【问题描述】:
我有一个记录模型和状态模型。每条记录的状态可以是“已批准”、“未批准”或“处理中”,它们存储在通过记录模型中的status_id连接的状态模型中。 这是我的模特和他们的关系。
Records(id, user_id, status_id, note, date, timestamp)
function status()
{
return $this->hasOne('App\Status');
}
Statuses(id, name)
function record()
{
return $this->belongsToMany('App\Record');
}
我想在我的视图 $records->status->name 上这样回调。但它给出了错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Statuses.record_id' in 'where clause' (SQL: select * from `statuses` where `statuses`.`record_id` = 3 and `statuses`.`record_id` is not null limit 1)
我尝试播放关系和回调,但它不起作用。有可能这样做吗?或者更好的是,我只是将状态直接保存在记录表上,而不使用状态记录。谢谢。
【问题讨论】:
-
你能告诉我们你的相关表名是什么吗?您是否还在迁移中设置了外键?
-
你能把你的迁移代码也放上去吗!
标签: php laravel eloquent relationship laravel-5.3