Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何根据第二个表的groupby检索三个表中的所有数据';s字段值是否使用Laravel雄辩查询?_Php_Laravel_Eloquent - Fatal编程技术网

Php 如何根据第二个表的groupby检索三个表中的所有数据';s字段值是否使用Laravel雄辩查询?

Php 如何根据第二个表的groupby检索三个表中的所有数据';s字段值是否使用Laravel雄辩查询?,php,laravel,eloquent,Php,Laravel,Eloquent,我有三个模型和一些方法,比如 1.员工模式 class雇员扩展模型 { 受保护的$fillable=[ “员工编号”、“卡片编号”、“未激活日期”、“激活日期”、“状态”, ]; 公共职能办公室(){ 返回$this->hasOne(EmployeeOffice::class); } 公共职能组(){ 返回$this->belongsTo('App\Hrm\Section'); } } 2.员工办公室模式 class EmployeeOffice扩展模型 { $fillable=[‘员工id’

我有三个模型和一些方法,比如

1.员工模式

class雇员扩展模型
{
受保护的$fillable=[
“员工编号”、“卡片编号”、“未激活日期”、“激活日期”、“状态”,
];
公共职能办公室(){
返回$this->hasOne(EmployeeOffice::class);
}
公共职能组(){
返回$this->belongsTo('App\Hrm\Section');
}
}
2.员工办公室模式

class EmployeeOffice扩展模型
{
$fillable=[‘员工id’、‘部门id’、‘行id’、‘加入日期’、‘总额’、‘确认日期’];
公共职能雇员(){
返回$this->belongsTo(Employee::class);
}
公共职能组(){
返回$this->belongsTo('App\Hrm\Section');
}
}
3.剖面模型

class Section extends Model
{
    protected $fillable = ['name','description','status'];

  //
}

我需要所有非活动员工信息根据员工非活动日期(来自员工模型)以及员工办公室模型中的他们的所有办公室信息,并且必须根据员工办公室模型中可用的分区id分组(分区id为外键)

为此,我必须有一些条件,比如

Employee::where('inactivedate','like','%.$date'%);
*并且需要office和employee表中的所有数据
*需要节名,并根据节模型中的节名进行分组
***请建议我如何解决这个问题***
试试这个:

$data = Section::with(
    array(
        'employeeOffice' => function(
            $query->with(
                'employees' => function(
                    $query->where('employees.inactivatedate', 'like', '%'.$date.'%'
                    )
            )
        )
    )
)
->get();
这将为每个
节\u id
提供一个数组。在这个数组中是
employeeOffice
s(1:n关系)。使用
with
的第二个查询将获取每个
employeeOffice
中的员工

但是,如果您正确定义了关系,这应该可以实现以下目的:

Section::with('EmployeeOffice.Employee')->get();

我通过这个解决了我的问题…

       if(Input::has('start') && Input::has('end')){
            $start = Carbon::parse(Input::get('start'))->startOfDay();
            $end = Carbon::parse(Input::get('end'))->endOfDay();

            $start = $start->format('Y-m-d');
            $end =  $end->format('Y-m-d');

           EmployeeOffice::query()->whereHas('employee',function ($query) use ($start,$end){
                $query->whereBetween('inactivedate',[$start, $end])->where('status',0);
            })->paginate()->groupBy('section_id');
        }

请检查此问题,因为我已编辑并详细描述了我的问题。我将检查您编辑的问题。根据加入部分编辑了我的答案。日期是什么样的?通常情况下,格式为YYYY-MM-DD。如果希望将所有日期都设置为一个月,可以使用
like
。如果没有,您应该使用正常的
where
=
。我的回答解决了您的问题吗?对不起,但仍然没有问题吗?@liqSTAR employee和employeeOffice模型具有一对一关系,而employeeOffice-to-section模型具有一对多关系。