2015-10-25 91 views
0

,我需要選擇在當年創造了記錄,以雄辯選擇創建本年度與雄辯

到目前爲止,所有的記錄,這是我使用的代碼。如何過濾結果以僅檢索當年創建的結果?

public function vacationsbalance($typeId) { 
    // Get vacataions balance for existing employee. 
    $vacationsBalance = $this->vacations()->where('vacation_type_id', '=', $typeId)->sum('days_num'); 
    return $vacationsBalance; 
} 

回答

4

假設你已經在你的表的時間戳(也就是你有一個created_at柱與記錄的創建日期),你可以使用:

public function vacationsbalance($typeId) { 
    // Get vacations balance for existing employee and for the current year. 
    return $this->vacations() 
     ->where('vacation_type_id', '=', $typeId) 
     ->whereRaw('year(`created_at`) = ?', array(date('Y'))) 
     ->sum('days_num'); 
} 

檢查Eloquent的文件和尋找whereRaw

+0

work perfect thanks milz –