繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM