简体   繁体   English

Laravel获取具有特定日期和月份的行

[英]Laravel get rows with specific day and month

I have the following query in laravel: 我在laravel中有以下查询:

$eventos = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
            ->where('tbl_users.birth_date', '>=', date("m-d"))  //my error
            ->where('tbl_events.year', '>=', date("Y"))
            ->get();

I have a users table ( tbl_users ), with the date of the birthdays of each user. 我有一个用户表( tbl_users ),其中包含每个用户的生日日期。 And an events table (tbl_events), which records the anniversary of each user. 和事件表(tbl_events),记录每个用户的周年纪念日。 Thus creating a table that will get the next birthdays. 从而创建一个可以获得下一个生日的桌子。

My birth_date is type "yyyy-mm-dd". 我的birth_date是“yyyy-mm-dd”。 The table events is the year . 桌面活动是year I want to get the next event on the current day and year. 我想在当天和今年举办下一场比赛。

ie I think the anniversary date must be greater than the current month and day, and the event with the top year to the current year 即我认为周年纪念日必须大于当前的月和日,以及当年的最高年份

Format the birth_date to mm-dd first before comparing it to date('m-d'), like so: 首先将birth_date格式化为mm-dd,然后再将其与日期('m-d')进行比较,如下所示:

$eventos = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
            ->whereRaw("DATE_FORMAT( tbl_users.birth_date, '%m-%d') >= ?", array(date('m-d')))
            ->where('tbl_events.year', '>=', date("Y"))
            ->get();

Update 更新

If your tbl_events doesn't have a full date attribute , you could do this in 2 queries: 如果您的tbl_events 没有完整的日期属性 ,则可以在2个查询中执行此操作:

First, get the remaining events of this year: 首先,获取今年剩余的事件:

$events_this_year = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
                ->whereRaw("DATE_FORMAT( tbl_users.birth_date, '%m-%d') >= ?", array(date('m-d')))
                ->where('tbl_events.year', '=', date("Y")) // Get remaining events this year
                ->get();

Second, get the events for next years: 第二,获得明年的活动:

$events_following_year = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
                    ->where('tbl_events.year', '>', date("Y")) // Get events for upcoming years
                    ->get();

Then merge them 然后合并它们

$eventos = $events_this_year->merge($events_following_year);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM