简体   繁体   English

Laravel Eloquent hasMany由父表过滤

[英]Laravel Eloquent hasMany filtered by parent table

In my system there are users, each of which has holiday allowances each year. 在我的系统中,有一些用户,每个用户每年都有假期津贴。

There are 3 tables (simplified): 有3个表(简化):

users 使用者
- id - ID
- name - 名称

user_leave_allowances user_leave_allowances
- id - ID
- user_id - 用户身份
- from -来自
- to - 至
- allowance -津贴

user_leave user_leave
- id - ID
- user_id - 用户身份
- from -来自
- to - 至
- days - 天

In the UserLeaveAllowance model I have set up the following relationship: 在UserLeaveAllowance模型中,我建立了以下关系:

public function leave(){
    return $this->hasMany('App\UserLeave','user_id','user_id');
}

And retrieving the leave allowance by the following: 并通过以下方式获取假期津贴:

$leaveAllowance = UserLeaveAllowance::with(['leave'])
->where('user_id',$id)
->get()

This pulls in the full list of leave allowances (user_leave_allowances) for that user along with all the leave they have currently requested (user_leave). 这将获取该用户的休假津贴(user_leave_allowances)的完整列表以及他们当前已请求的所有休假(user_leave)。

However, the allowances will be for different periods of time, eg 1/1/2017 - 31/12/2017 and leave will be taken out of these periods. 但是,津贴将针对不同的时间段,例如1/1/2017-31/12/2017,并将在这些时间段内休假。 So for each leave allowance period I want just the leave that falls within those two dates (from & to), at the moment it just pulls through all the leave for that user into each leave allowance. 因此,对于每个休假津贴期,我只希望属于这两个日期(从&到)内的休假,此刻,它只是将该用户的所有休假纳入每个休假津贴。

I have tried many different ways around it, using the where conditions on the hasMany as such... 我已经尝试了许多不同的方法,例如使用hasMany的where条件。

public function used(){
    return $this->hasMany('App\UserLeave','user_id','user_id')
->where('from', '>=', 'user_leave_allowances.from')
->where('to', '<=', 'user_leave_allowances.to');
}

and

public function used(){
    return $this->hasMany('App\UserLeave','user_id','user_id')
->whereRaw('user_leave.from >= user_leave_allowances.from')
->whereRaw('user_leave.to <= user_leave_allowances.to');
}

But I just can't figure out a way to get this to work. 但是我只是想不出一种方法来使它工作。 I'm fairly new to Laravel and trying to get to grips with Eloqent and relationships, I'm sure the answer isn't too far away. 我对Laravel并不陌生,想与Eloqent和人际关系保持紧密联系,我相信答案不会太遥远。

Here's my suggestion: 这是我的建议:

users - id - name 用户 -ID-名称

user_leave_allowances - id - user_id - from - to user_leave_allowances -id- user_id-从-到

user_leave - id - user_leave_allowances_id - from - to user_leave -id-user_leave_allowances_id-从-到

In short relate your leave with your leave allowances rather than the user directly because in essence a leave request is taken out of the allowance not out of the user directly. 简而言之,请假与您的请假津贴而不是与使用者直接联系,因为从本质上说,请假是从津贴中提取的,而不是直接从使用者中提出的。

class User extends Model {
     public leaveAllowance() {
         return $this->hasMany(UserLeaveAllowance::class);      
     }        
} 

class UserLeaveAllowance {
      public user() { return $this->belongsTo(User::class); }
      public leaveRequests() { 
        return $this->hasMany(UserLeave::class);
      }
}

class UserLeave {
      public allowancePeriod() { 
        return $this->belongsTo(UserLeaveAllowance::class);
      }
} 

Now you can do things like: 现在您可以执行以下操作:

UserLeaveAllowance::whereHas("user", function ($q) { 
        return $q->where("id", <user id>); //Allowances for this user
})->leaveRequests() 
->selectRaw("SUM(TIMESTAMPDIFF(DAY, to, from)) as days")
->value("days");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM