I am working on a booking app, I get a start and end (datetime) from a user and check if this period (start & end) intersect with the database columns (start & end) so I need to compare Input['start']
and Input['end']
with the database, so I used Carbon::create($inputstart)->between($start ,$end));
but it alwayes gives errores "Unexpected data" & "Trailing data"
Here is the controller method to check and store the input
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']);}
}
}
I also declared the database columns in the model as dates
protected $dates = ['start', 'end'];
How can I pass datetime format from HTML form and database "datetime" so Carbon can do the "between" comparison ?
It looks like you already have created your Carbon instance for the $end variable with the line
$end = Carbon::parse($end);
So, if you are wanting to determine if $end is between $bstart and $bend, then you would want your line to look like this
$end->between($bstart, $bend);
If this doesn't work, dd($end)
along with dd($bstart)
and dd($bend)
to make sure they are all Carbon objects.