I want to achieve grouping on joined fields using Laravel's eloquent ORM with eager loading.
I have 2 models defined as follows:
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';
}
I'm trying to join both the tables, and group by 'id' of department with eager loading
$User::with('department')->groupBy('department_id');
I'm getting error "department_id column not found". I'm expecting to run the following MySql query:
select * from user inner join department inner join user.belongs_to_department_id=department.department_id group by department.department_id;