Laravel 5.5 - 哪里有2天?

时间:2018-02-21 01:14:26

标签: laravel laravel-5 eloquent laravel-5.5

使用eloquent,我试图让所有用户在2月2829(如果是闰年)注册时注册:

// If leap year, get feb 28 and 29 users
$users = \App\User::->whereMonth('created_at', '=', '02')
                    ->whereDay('created_at', '=', '28')
                    ->whereDay('created_at', '=', '29')
                    ->get();

当我们在此处添加额外的29支票时,上述功能无效:

->whereDay('created_at', '=', '29') // does not work because of whereDay '28' above

理想情况下,这样的东西会起作用,但它在api中不存在:

->whereDayIn('created_at', ['28','29'])

知道如何在2月28 / 29这两天内吸引用户?

2 个答案:

答案 0 :(得分:2)

试试这个:

$users = \App\User::whereMonth('created_at', '=', '02')
                    ->whereIn(DB::raw('DAY(created_at)'), [28,29])
                    ->get();

答案 1 :(得分:1)

将您的查询更改为:

// If leap year, get feb 28 and 29 users

$days = [28, 29];

$users = \App\User::whereMonth('created_at', '02')
                    ->where(function($query) use($days) {
                        foreach ($days as $day) {
                            $query->whereDay('created_at', '=', $day, 'or');
                        }
                    })
                    ->get();

OR

// If leap year, get feb 28 and 29 users

$days = [28, 29];

$users = \App\User::whereMonth('created_at', '02')
                    ->whereIn(DB::raw('DAYOFMONTH("created_at")'), $days)
                    ->get();