【发布时间】:2018-05-27 21:11:56
【问题描述】:
我的应用程序的目的是按部门、日期(每天、每周、每月、每季度和每年)处理工资单数据,并将其显示在带有图表的仪表板中。
我有两 (2) 个表格,我上传 CSV 以获取数据:'attendance' 和 'payroll';
我有十 (10) 个学校,每个学校都由文本字段中的四 (4) 个数字标识。
**attendance** = date | school_code | department_name | child_count
**payroll** = date | school_code | teacher_name | department | hours | pay_rate
这就是我正在尝试的。我的查询非常昂贵。
public function index() {
$schools = array( '1001', '1003', '1004', '1005', '1007', '1008', '1009', '1010', '1011' );
$infants = 'Infants';
$ones = 'Ones';
$twos = 'Twos';
$threes = 'Threes';
$fours = 'Fours';
$fives = 'Fives';
$schoolAge = 'School Age';
$unknowns = 'Unknown';
$preK = 'PreK';
$office = 'Office';
$oneInfants = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $infants );
$oneOnes = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $ones )->get();
$oneTwos = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $twos )->get();
$oneThrees = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $threes )->get();
$oneFours = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $fours )->get();
$oneFives = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $fives )->get();
$oneSchoolAges = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $schoolAge )->get();
$oneUnknowns = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $unknowns )->get();
$onePreK = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $preK )->get();
$oneOffice = ChildCount::where( 'school_code', $schools[0] )->where( 'department_name', $office )->get();
... **REPEATED FOR EACH SCHOOL** ...
$oneMaster= DB::table( 'payrolls' )
->join( 'child_counts', 'child_counts.school_code', '=', 'payrolls.school_code' )
->where( 'payrolls.department', $infants )
->where('payrolls.school_code',$schools[0])
->take( 100 )
->select( 'payrolls.*', 'child_days' )
->get();
我没有适用于学校的模型,因为这是一个第三方应用程序,数据从我们的主要运营计划导出为 CSV。
如何在加入 child_counts && payrolls 之间的 school_code 时优化这些查询 - 一旦我能够遍历这些学校,部门和日期——我可以每天、每周、每月、每季度和每年查询;
提前致谢。
【问题讨论】:
标签: php mysql laravel csv eloquent