我有一个方法可以接收一个"月份(month)"和另一个"年份(year)"的参数,你可以接收其中一个或两个参数。
如果只接收到了月份,我想要进行一次查询,在一个名为"created_at"的字段中查找该字段日期的月份。
目前我使用这段代码,但是进行模糊匹配时不能正确执行。
如果只接收到了月份,我想要进行一次查询,在一个名为"created_at"的字段中查找该字段日期的月份。
目前我使用这段代码,但是进行模糊匹配时不能正确执行。
else if ($request->has('month')) {
$month = $request->input('month');
$currentTimestamp = date_create_from_format('n', $month)->getTimestamp();
$currentDate = date('m', $currentTimestamp);
$filters['updated_at'] = $currentDate;
unset($filters['month']);
}
$cars = Car::where(function($query) use($filters) {
foreach ($filters as $column => $value) {
if ($column === 'updated_at') {
$query->where($column, 'LIKE', '%'.$value.'%');
continue;
}
$query->where($column, 'LIKE', '%'.$value.'%');
}
})->orderBy('updated_at', 'desc')->get();
created_at
是一个时间戳列吗? - Jonas Staudenmeir