简体   繁体   中英

How to select request date is between two dates for PHP Laravel?

Currently, I have two dates inside the database such as depart_date and return_date. For the view blade, the user must select the accident_date which is the accident_date must be between depart_date and return_date. So, before the the user submit the form, the form will check either the accident_date is exist between depart_date and return_date or not. I have created the sql command inside the controller. But, it gave me an error.

Error : syntax error, unexpected ''=>'' (T_CONSTANT_ENCAPSED_STRING), expecting ')'

Controller:

$startdate = InsuranceEnrollment::select('depart_date')
             ->where($request->accident_date '=>', 'depart_date') AND ($request->accident_date 
             '<=', 'return_date');

dd($startdate);

View blade:

<div class="form-group">
<label class="form-control-label form-control-sm">Accident </label>
<div class="input-group">
<input type="text" placeholder="" class="form-control date-picker" 
data-datepicker-color="primary" name="accident_date" required>
</div> 

The comparison condition need to be like this, so the $request->accident_date will between these two dates.

depart_date <= accident_date AND return_date >= accident_date

You need to use WHERE ... AND... like this:

$startdate = InsuranceEnrollment::select('depart_date')
                                ->where('depart_date', '<=', $request->accident_date)
                                ->where('return_date', '>=', request->accident_date);

Your where clause is not in proper way, here's how to do it.

 $startdate = InsuranceEnrollment::select('depart_date')->where([
     'depart_date' <= $request->accident_date,
     'return_date' => $request->accident_date,
 ])->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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