我有一个原生的Php / Mysql查询,我需要将其转换为Laravel查询。我在laravel的世界里很新,发现它很有趣,但我在这个问题上已经超过3天。
这是我的母语Php / Mysl查询
$sql = "SELECT a.employeeID, a.nric, a.fullName, b.salary, c.total_allowance, c.year, c.month, c.period, d.deptName, c.aws, c.additionals,
c.id, a.date_of_birth, c.total_additional
from employees a
LEFT OUTER JOIN salary b ON a.employeeID = b.employeeID
LEFT OUTER JOIN payrolls c ON a.employeeID = c.employeeID and year = '$presyear' and month = '$presmonth' and period = '$period'
LEFT OUTER JOIN department d ON a.designation = d.id
UNION
SELECT a.employeeID, a.nric, a.fullName, b.salary, c.total_allowance, c.year, c.month, c.period, d.deptName, c.aws, c.additionals,
c.id, a.date_of_birth, c.total_additional
from employees a
LEFT OUTER JOIN salary b ON a.employeeID = b.employeeID
LEFT OUTER JOIN payrolls c ON a.employeeID = c.employeeID and year != '' and month != '' and period != ''
LEFT OUTER JOIN department d ON a.designation = d.id
WHERE c.net_salary != ''";
它给了我想要的结果,但是我不知道你们可能想要对这个进行一些调整。
现在这是我在Lravel中可以得到的
$result = Payroll::select('payrolls.id','payrolls.employeeID','fullName','month','year','period','department.deptName','payrolls.aws',
'payrolls.adjustment','payrolls.allowances','payrolls.alencashment','payrolls.housing_allowance',
'payrolls.med_reimburse','payrolls.overtime_hours','payrolls.overtime_pay','payrolls.reimbursement','payrolls.tips',
'net_salary','payrolls.total_additional','payrolls.total_allowance','payrolls.total_deduction','payrolls.total_cpf')
->join('employees', 'payrolls.employeeID', '=', 'employees.employeeID','left')
->join('department', 'employees.designation', '=', 'department.id','left')
->where('payrolls.year','=',$presyear)
->where('payrolls.month','=',$presmonth)
->where('payrolls.period','=',$period);
我不知道如何做联盟以及外部联接中的所有内容和我在本机中所做的一样。
我使用的是Laravel 4.2.17
感谢。
答案 0 :(得分:0)
我不证明这一点,但你可以做类似
的事情 $first = \DB::table('employees')->select(['employees.employeeID', 'employees.nric', 'employees.fullName', 'salary.salary', 'payrolls.total_allowance', 'payrolls.year', 'payrolls.month', 'payrolls.period', 'department.deptName', 'payrolls.aws', 'payrolls.additionals', 'payrolls.id', 'employees.id', 'employees.date_of_birth', 'payrolls.total_additional'])
->leftJoin('salary', 'employees.id', '=', 'salary.employeeId')
->leftJoin('payrolls', function($join)
{
$join->on( 'employees.id', '=', 'payrolls.employeeId')->on('year', '!=', '')->on('month', '!=', '')->on('period', '!=', '');
})
->leftJoin('department', 'employees.designation', '=', 'department.id')
->where('payrolls.net_salary', '!=', '');
\DB::table('employees')->select(['employees.employeeID', 'employees.nric', 'employees.fullName', 'salary.salary', 'payrolls.total_allowance', 'payrolls.year', 'payrolls.month', 'payrolls.period', 'department.deptName', 'payrolls.aws', 'payrolls.additionals', 'payrolls.id', 'employees.id', 'employees.date_of_birth', 'payrolls.total_additional'])
->leftJoin('salary', 'employees.id', '=', 'salary.employeeId')
->leftJoin('payrolls', function($join)
{
$join->on( 'employees.id', '=', 'payrolls.employeeId')->on('year', '=', $presyear)->on('month', '=', $presmonth)->on('period', '=', $period);
})
->leftJoin('department', 'employees.designation', '=', 'department.id')->union($first)->get();
请参阅https://laravel.com/docs/4.2/queries。
如果你不能继续,你可以随时使用\ DB :: select看https://laravel.com/docs/4.2/database#running-queries。