[英]Next record from this day in Laravel
我是 Laravel 的初學者。 我在我的項目中使用 Laravel 5.8。
我有這個代碼:
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();
}
這段代碼工作正常。 我需要顯示今天或第二天將發生的所有記錄(事件)。
我怎樣才能做到?
僅獲取今天的記錄
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();
}
獲取明天的記錄
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();
}
按年份范圍獲取
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.