简体   繁体   中英

Next record from this day in Laravel

I am beginner in Laravel. I use Laravel 5.8 in my project.

I have this code:

class EventCalendar extends Model
{
    use scopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['email_responsible_person','www_responsible_person','phone_responsible_person','responsible_person', 'company_id', 'id_category', 'id_place', 'enable', 'title', 'title_on_the_list',  'content', 'short_content', 'url_address', 'date_from', 'date_to', 'hour_from', 'hour_to', 'price', 'file', 'hide_data', 'visible_on_promo_box', 'date'];
    public $timestamps = false;

    public function category()
    {
        return $this->belongsTo('App\EventCalendarCategory', 'id_category');
    }

    public function localization()
    {
        return $this->belongsTo('App\EventCalendarPlace', 'id_place');
    }

}


public function getNextEventsList()
    {
        return EventCalendar::active()->with(['localization', 'category'])->where('visible_on_promo_box', '=', 1)->orderBy('date_from', 'ASC')->get();
    }

This code work fine. I need to show all records (events) that will take place today or the next day.

How can I make it?

Get records only for today

use Carbon\Carbon;

public function getNextEventsList()
{
  return EventCalendar::active()->with(['localization', 'category'])->where('visible_on_promo_box', '=', 1)->where('date_from', Carbon::today())->orderBy('date_from', 'ASC')->get();
}

Get records for tomorrow

use Carbon\Carbon;

public function getNextEventsList()
{
  return EventCalendar::active()->with(['localization', 'category'])->where('visible_on_promo_box', '=', 1)->where('date_from', Carbon::tomorrow())->orderBy('date_from', 'ASC')->get();
}

Get by year range

use Carbon\Carbon;

public function getNextEventsList()
{
  return EventCalendar::active()->with(['localization', 'category'])->where('visible_on_promo_box', '=', 1)->where('date_from', '>=', Carbon::now())->where('date_from', '<=', Carbon::now()->addYear())->orderBy('date_from', 'ASC')->get();
}

获取今天或第二天将发生的记录。

EventCalendar::active()->with(['localization', 'category'])->where('visible_on_promo_box', '=', 1)->whereIn('date_from',[Carbon::today(),Carbon:tomarrow()])->get()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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