简体   繁体   English

从今天开始在 Laravel 中的下一个记录

[英]Next record from this day in Laravel

I am beginner in Laravel.我是 Laravel 的初学者。 I use Laravel 5.8 in my project.我在我的项目中使用 Laravel 5.8。

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()

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

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