使用Eloquent选择当前创建的所有记录

Mag*_*mid 1 select eloquent laravel-4

我需要用Eloquent选择当年创建的记录

到目前为止,这是我正在使用的代码.如何过滤结果以仅检索当前年份中创建的结果?

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

Luí*_*ruz 6

假设您的表中有时间戳(也就是说,您有一个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');
}
Run Code Online (Sandbox Code Playgroud)

查看Eloquent的文档并查找whereRaw.