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
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:
steps_id
from job
table. job_id
in steps
table as a foreign key. Try it
You are set wrong relationship on model
Job Model
public function steps ()
{
return $this->belongsTo('MyfirstApp\Step');
}
Step Model
public function Job ()
{
return $this->hasMany('MyFirstApp\Job');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.