2016-12-06 61 views
1

我在預訂應用程序的工作,我從用戶那裏得到一個開始和結束(日期時間),並檢查這個期間(開始&末)與數據庫中的列(開始&結束)相交,所以我需要比較Input['start']Input['end']與數據庫,所以我用Carbon::create($inputstart)->between($start ,$end));但alwayes給errores「意外的數據」 &「尾隨數據」Laravel Carbon,如何格式化以比較日期時間?

這裏是控制器的方法來查看和存儲輸入

public function store(Request $request,$yard,$court) 
{ 
    $start =$request->input("start"); 
    $end =$request->input("end"); 
    $courtCalendar = CalendarEvent::where('court_id',$court)->get(); 
    $start= Carbon::parse($start); 
    $end = Carbon::parse($end); 
    foreach ($courtCalendar as $booked) {  
     $bend= Carbon::parse($booked->end); 
     $bstart= Carbon::parse($booked->start); 
     dd(Carbon::create($end)->between($bstart ,$bend)); 
     if (!Carbon::create($start)->between($bstart ,$bend)){ 
      if (!Carbon::create($end)->between($bstart ,$bend)) { 
    $calendar_event = new CalendarEvent(); 
    $calendar_event->user_id   = Auth::user()->id; 
    $calendar_event->court_id   = $court; 
    $calendar_event->title   = $request->input("title"); 
    $calendar_event->start   = $start; 
    $calendar_event->end    = $end; 
    $calendar_event->status   = 0; 
    $calendar_event->save(); 
    return redirect()->back() 
    ->with(['header'=>'Success!','class' =>'alert alert-success','message'=> 'booked successfully.', 
     'calendar'=>$calendar_event]); 
      }else 
      {return redirect()->back() 
       ->with(['header'=>'Warning!','class' =>'alert alert-warning','message'=> 'cannot book at this time end time is occupied']);} 
     }else 
      {return redirect()->back() 
       ->with(['header'=>'Warning!','class' =>'alert alert-warning','message'=> 'cannot book at this time start time is occupied']);} 
    } 
} 

我也宣告了數據庫列在模型中,日期爲

protected $dates = ['start', 'end']; 

如何從HTML表單和數據庫「datetime」傳遞datetime格式,以便Carbon可以執行「between」比較?

回答

5

看起來你已經與行創建了碳的實例爲$結束變量

$end = Carbon::parse($end); 

所以,如果你是想確定是否$到底是$ bstart和$彎之間,那麼你希望你的線看起來像這樣

$end->between($bstart, $bend); 

如果這不起作用,dd($end)dd($bstart)dd($bend)一起,以確保它們都是碳的對象。

+0

謝謝@MMMTroy,它的工作 –

+1

謝謝[MMMTroy](http://stackoverflow.com/a/41002733/3452348)這個超級回答。 @BeshoyTamry請授予他[接受的答案](http://meta.stackexchange.com/a/5235/294451) – Zl3n