dtnat7146 2015-03-23 05:20
浏览 23
已采纳

Laravel 5 | 一对多关系查询错误

I've created a one-to-many relationship between a Jobs Model and Permits model. I've recently discovered the awesome tool of Tinker so I've been using it to test my models. When I run Job::with('steps')->find(1); I get this error

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))'

Here's my Job Model

   public function steps ()
    {
        return $this->hasMany('MyfirstApp\Step');
    }

Here's my Step Model

   public function Job ()
    {
      return $this->belongsTo('MyFirstApp\Job');
    }

I've already set up the foregin key in the Jobs Table so I'm not sure what the error can be. Any ideas?

Table Structure for reference enter image description here

  • 写回答

2条回答 默认 最新

  • dongxiezhuo8852 2015-03-23 07:39
    关注

    For your relationship, Job has many Steps, you have assigned wrong foreign key.

    In your case steps table should contain job_id rather than job contains steps_id.

    Solution:

    • Remove steps_id from job table.
    • Set job_id in steps table as a foreign key.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探