Laravel 5.3关系问题,未找到列


Laravel 5.3 relationship issue, Column not found

class Company extends Model
{
 public function employees() {
    return $this->hasMany('Employee');   
}
}
class User extends Model
{
public function employee()
{
    return $this->hasMany('Employee');
}
}
class Employee extends Model
{
protected $table = "user_role_company";
public function user(){
    return $this->belongsTo('User');
}
public function company(){
    return $this->belongsTo('Company');
}
}

我得到"列未找到:1054未知列'user_id'在'where子句' (SQL: select * from companies where user_id = 55)"时运行:

公司::与("雇员")-> (user_id, $ user -> id) -> ();

我做错了什么?

谢谢

您可以尝试如下:

Company::whereHas('employees', function($query) use($user) {
           $query->where('user_id', $user->id);
        })
        ->with('employees')
        ->get();

使用闭包在with上使用filter

Company::with(['employees' => function($q) use($user) {
    return $q->where('user_id', $user->id);
}]);