I have the following database schema set up in a Laravel app. I'm only mentioning the important columns for simplicity sake.
Task
- job_id
Job
- client_id
User
- id
Inside the Task model I want to have a relationship method to the users table, which in this case is referred to as the Client (client_id) on the intermediate jobs table.
In my code I want to able to reference $task->client();
I've looked through the docs and found either:
public function client()
{
return $this->belongsToMany('App\User', 'users', 'client_id');
}
returns:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jobs.user_id' in 'field list' (SQL: select
users.*,jobs.client_idaspivot_client_id,jobs.user_idaspivot_user_idfromusersinner joinjobsonusers.id=jobs.user_idwherejobs.client_id= 112 andusers.deleted_atis null)"
or
public function client()
{
return $this->hasOneThrough('App\User', 'App\Job', 'client_id', 'user_id');
}
returns:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.user_id' in 'on clause' (SQL: select
users.*,jobs.client_idaslaravel_through_keyfromusersinner joinjobsonjobs.id=users.user_idwherejobs.deleted_atis null andjobs.client_id= 111 andusers.deleted_atis null limit 1)"
How can I retrieve the User model from the Task model?