2016-08-02 62 views
0

我有一个appointments table其中我将预约开始日期和时间存储在datetime字段中。我有很多方法可用于获取从特定日期开始或在两个给定日期之间开始的约会。例如:Laravel在日期之间搜索没有结果时间相同(从今天获得结果)

日历类:

public function today() 
{ 
    $start = Carbon::now()->hour('00')->minute('00')->second('00'); 
    $end = Carbon::now()->hour('23')->minute('59')->second('00'); 

    return Appointment::getAppointments($this->start, $this->end); 
} 

// public function tomorrow(); 
// public function thisMonth(); 

预约型号:

public static function getAppointments($start, $end) 
{ 
    return static::whereBetween('date', array(
     $start->format('Y-m-d H:i'), 
     $end->format('Y-m-d H:i') 
    ))->get(); 
} 

正如你看到的,我需要设置小时,分钟和秒,因为否则将不会返回任何结果,如果开始日期和结束日期相同。是否可以简单地做到以下几点,并仍然从那个日期获得结果?

public function today() 
{ 
    $start = Carbon::now(); 
    $end = Carbon::now(); 

    return Appointment::getAppointments($this->start, $this->end); 
} 

public static function getAppointments($start, $end) 
{ 
    return static::whereBetween('date', array(
     $start->format('Y-m-d'), 
     $end->format('Y-m-d') 
    ))->get(); 
} 

回答

1

尝试下面的代码

return static::whereBetween(Db:raw('DATE(date)'), array(
    $start->format('Y-m-d'), 
    $end->format('Y-m-d') 
))->get(); 

这将铸就DATETIME领域迄今为止

+0

实际上,我用这个日期间下跌寻找生日,但按月和日。不知道我也可以用这样的约会这样的。太棒了,谢谢:) – Hardist

1

我认为这个问题是您的日期字段最有可能有你需要截断,如果时间分量你要比较它是否在日期之间。

确保在您的控制器顶部

use DB; 

这应该将日期转换为年月日导入数据库命名空间,这样它会

public static function getAppointments($start, $end) 
{ 
    return static::whereBetween(DB::raw('CAST(date as DATE)', array(
     $start->format('Y-m-d'), 
     $end->format('Y-m-d') 
    ))->get(); 
} 
+0

非常感谢,很好的回答以及:) – Hardist

相关问题