Laravel 5.3关系问题,未找到列

时间:2016-11-06 20:13:04

标签: php mysql laravel

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

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

公司::用( '雇员') - 化合物其中( 'USER_ID',$用户> ID) - >得到();

我做错了什么?

由于

2 个答案:

答案 0 :(得分:0)

使用闭包在filter上使用with

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

答案 1 :(得分:0)

您可以尝试:

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