[英]Laravel 5 | One to Many Relationship Query Error
我已经在Jobs模型和Permits模型之间建立了一对多的关系。 我最近发现了Tinker的强大工具,因此我一直在使用它来测试我的模型。 当我运行Job::with('steps')->find(1);
我得到这个错误
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'steps.job_id' in 'where clause' (SQL: select * from `steps` where `steps`.`job_id` in (1))'
这是我的工作模式
public function steps ()
{
return $this->hasMany('MyfirstApp\Step');
}
这是我的脚步模型
public function Job ()
{
return $this->belongsTo('MyFirstApp\Job');
}
我已经在Jobs Table中设置了foregin键,所以我不确定会出现什么错误。 有任何想法吗?
表结构供参考
对于您的关系, Job
有很多Steps
,您分配了错误的外键。
在您的情况下, steps
表应包含job_id
而不是job
包含steps_id
。
解:
job
表中删除steps_id
。 steps
表中将job_id
设置为外键。 试试吧
您在模型上设置了错误的关系
工作模式
public function steps ()
{
return $this->belongsTo('MyfirstApp\Step');
}
步模型
public function Job ()
{
return $this->hasMany('MyFirstApp\Job');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.