按月份过滤,格式为“Y-m-d”
Posted
技术标签:
【中文标题】按月份过滤,格式为“Y-m-d”【英文标题】:Filter by month with format 'Y-m-d' 【发布时间】:2017-11-23 05:32:21 【问题描述】:我有一个date
,格式为Y-m-d
。如何按月过滤?我用 January = 1, February = 2
等数字指定月份。
public function scopegetmonthleavereport($query,$payroll_employee_id,$month)
$query->join('tbl_payroll_leave_employee_v2','tbl_payroll_leave_schedulev2.payroll_leave_employee_id','=','tbl_payroll_leave_employee_v2.payroll_leave_employee_id')
->join("tbl_payroll_employee_basic","tbl_payroll_leave_employee_v2.payroll_employee_id","=","tbl_payroll_employee_basic.payroll_employee_id")
->join("tbl_payroll_leave_tempv2","tbl_payroll_leave_employee_v2.payroll_leave_temp_id","=","tbl_payroll_leave_tempv2.payroll_leave_temp_id")
->select(DB::raw('tbl_payroll_employee_basic.payroll_employee_id , tbl_payroll_leave_schedulev2.payroll_schedule_leave, tbl_payroll_employee_basic.payroll_employee_display_name, tbl_payroll_leave_schedulev2.payroll_leave_temp_with_pay, tbl_payroll_leave_tempv2.payroll_leave_type_id, tbl_payroll_leave_employee_v2.payroll_leave_employee_id, tbl_payroll_leave_schedulev2.consume, tbl_payroll_leave_employee_v2.payroll_leave_temp_hours, sum(tbl_payroll_leave_schedulev2.consume) as total_leave_consume, (tbl_payroll_leave_employee_v2.payroll_leave_temp_hours - sum(tbl_payroll_leave_schedulev2.consume)) as remaining_leave'))
->groupBy('tbl_payroll_leave_employee_v2.payroll_leave_temp_id')
->where('tbl_payroll_leave_schedulev2.payroll_leave_schedule_archived',0)
->where('tbl_payroll_leave_employee_v2.payroll_employee_id', $payroll_employee_id)
->whereBetween('tbl_payroll_leave_schedulev2.payroll_schedule_leave', $date); //where i want to put the date
return $query;
【问题讨论】:
上帝查询构建器是如此ugly
但在 SQL 中你会这样做 WHERE MONTH(date_field) = :month
提醒我我必须使用 SQL lexer/parser 构建我的 ORM...
@ArtisticPhoenix 我如何在 laravel bro 中用 eloquent 做到这一点?哈哈
我不知道我不使用 Laravel,但该查询构建器在我看来就像 crap
(从可读性的角度来看)。我写 SQL 已经 7 年了,whereMonth
我是用这条线做的哈哈。谢谢你的线索兄弟
当然,我希望我能直接告诉你。但我使用基础知识,这样看起来总是更干净。我的意思是我得到了 ORM,我已经 regretfully
使用过它们( Doctrine ),我发现它们对我来说就像一副手铐。
【参考方案1】:
试试like和regex里面的where, 使用这样的东西:
->Where('date', 'like', '%-' .$month. '-%')->get();
我没试过。
【讨论】:
【参考方案2】:use Carbon\Carbon;
public function index()
date_default_timezone_set('Asia/Kolkata');
//Total Students
$total_students = User::where('role_id','2')->count();
$today = Carbon::today()->toDateTimeString();
//dd($today);
$first_day = date('Y, n, j', strtotime('-1 day', strtotime($today)));
$second_day = date('Y, n, j', strtotime('-2 day', strtotime($today)));
$third_day = date('Y, n, j', strtotime('-3 day', strtotime($today)));
$fourth_day = date('Y, n, j', strtotime('-4 day', strtotime($today)));
$fifth_day = date('Y, n, j', strtotime('-5 day', strtotime($today)));
$sixth_day = date('Y, n, j', strtotime('-6 day', strtotime($today)));
$seventh_day = date('Y, n, j', strtotime('-7 day', strtotime($today)));
return view('home',compact('total_students','total_doner','current_day_student','first_day','second_day','third_day','fourth_day','fifth_day'
,'sixth_day','seventh_day'));
【讨论】:
这个例子对你有帮助以上是关于按月份过滤,格式为“Y-m-d”的主要内容,如果未能解决你的问题,请参考以下文章