2015-10-25 24 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 –