2017-02-23 72 views
1

我有一個本地Php/Mysql查詢,我需要轉換爲Laravel查詢。我在laravel的世界裏很新,發現它很有趣,但我在這個問題上已經超過3天了。Laravel聯盟與加入選項

這裏是我的家鄉PHP /思想報查詢

$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