Laravel通过连接(内部连接)字段进行优先分组,并进行快速加载

时间:2019-01-31 04:48:45

标签: php laravel eloquent eager-loading

我想使用渴望加载的Laravel雄辩的ORM在联接字段上实现分组。

我有2个定义如下的模型:

class User extends Model{
  public $table = 'user';
  public function department(){
    return $this->belongsTo('models/Department','department_id','belongs_to_department_id');
  }
}
class Department extends Model{
  public $table = 'department'; 
}

我正试图同时加入两个表,并按急切加载部门的'id'分组

$User::with('department')->groupBy('department_id');

我收到错误消息“找不到department_id列”。 我期望运行以下MySql查询:

select * from user inner join department inner join user.belongs_to_department_id=department.department_id group by department.department_id;

0 个答案:

没有答案