Laravel Eloquent按日期范围搜索

时间:2017-07-30 21:40:38

标签: mysql laravel date search eloquent

我有两个表“patient”和“booking”表,并且它们之间存在“一对多”的关系,我在index_booking页面中创建了一个搜索表单,用户可以在其上键入patient_name以进行搜索根据WHERE条件的用户名也按日期范围搜索,但是在按日期搜索之前没有数据出现在表格中,所以我不知道为什么默认情况下根据WHERE条件不显示数据:

这是预订模式

class Booking extends Eloquent
{

    public function patient()
    {
      return $this->belongsTo('App\Patient'); 
    }

    public function user()
    {
      return $this->belongsTo('App\User'); 

    }
}

这是患者模型

class Patient extends Eloquent
{
    public function booking()
    {
      return $this->hasMany('App\Booking'); 
    }

    public function user()
    {
      return $this->belongsTo('App\User'); 

    }
}

我在预订的索引页面中使用了这段代码

   <div class="search1">
           {!! Form::open(['method'=>'GET','url'=>'booking','class'=>'navbar-form navbar-left','role'=>'search'])  !!}
           <!--<span class="badge">{{$bookings->count()}}</span>-->

       </div>
  <tr>

   <div class="col col-md-3">
     <p></span><input type="text" id="end" name="end" placeholder="الي تاريخ"><span class="glyphicon glyphicon-calendar"></p> 
   </div>

   <div class="col col-md-4">
     <p><input type="text" id="start" name="start" placeholder="من تاريخ"></p> 
   </div>
    <td class="title search col-md-5">
    <div class="input-group custom-search-form">
     <input type="text" class="form-control" name="search" placeholder="ابحث بالاسم...">
     <span class="input-group-btn">
         <button class="btn btn-default-sm" type="submit">
            <i class="fa fa-search"> <span class="hiddenGrammarError" pre="" data-mce-bogus="1"></span></i>
        </button>
    </span>

</div>
    </td>
    {!! Form::close() !!}

我在Booking Controller中使用此代码根据Where显示数据  条件和按日期范围搜索:

       public function index(Request $request)
    {


$search = Request::get('search');
$start = Request::input('start');
$end = Request::input('end');

$bookings = Booking::whereHas('patient', function ($query) use ($search, $start, $end) {
$query->where('patient_name', 'like', '%' . $search . '%')
      ->whereBetween(DB::raw('date(booking_date)'), [$start, $end]) ;


})->where('status','=', null)->whereHas('patient', function ($query){
  $query->where('company_id','=' ,Auth::user()->company_id); 

})->orderBy("id", "asc") 
  ->paginate(7);
  return view('booking.index')->withBookings($bookings);


}


    }

预订索引中的Javascript代码为:

 <script>
          $(function(){$("#start").datepicker({
            autoclose:!0,
            dateFormat:"yy-mm-dd",
            startDate:new Date
        }),
        $("#end").datepicker({
            autoclose:!0,
            dateFormat:"yy-mm-dd",
            endDate:new Date
        })
    }); 

0 个答案:

没有答案