我正在表格中显示数据。我的故事有签到时间。现在,我要过滤两个日期之间的数据。我从日期和日期添加了jQuery日期时间选择器。但是我很困惑,只能在同一张表中显示日期范围数据,默认情况下显示所有数据。有人可以建议我使用日期范围过滤器的模型视图和控制器的确切方法吗?
日期范围输入
<div class="col-md-4">
<div class="input-group date form_meridian_datetime" data-date="2012-12-21T15:25:00Z">
<input type="text" placeholder="from" name="from" id="from" size="16" class="form-control">
<span class="input-group-btn">
<button class="btn default date-reset" type="button">
<i class="fa fa-times"></i>
</button>
<button class="btn default date-set" type="button">
<i class="fa fa-calendar"></i>
</button>
</span>
</div>
表
<table class="table table-striped table-bordered table-hover" id="sample_3">
<thead>
<tr>
<th> Permit Taker </th>
<th> Permit By </th>
<th> Permit Types </th>
<th> Purpose </th>
<th> Entry Date &Time </th>
<th> Exit Date & Time </th>
<th> SignIn </th>
<th> SignOut </th>
<th> Edit </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
@foreach($internalVisitors as $internalVisitor)
<tr>
<td> {{ $internalVisitor->internalHosts->hostName }} </td>
<td> {{ $internalVisitor->permitBy}} </td>
<td> {{ $internalVisitor->permitType}} </td>
<td> {{ $internalVisitor->purpose}} </td>
<td> {{ $internalVisitor->startDate}} </td>
<td> {{ $internalVisitor->endDate}} </td>
<td>
@if ( $internalVisitor-> signIn === 1)
<span >{{ $internalVisitor->signInTime}}</span>
@else
<form method="post" action="{{route('signInUpdate','id')}}">
{{ csrf_field()}}
<input type="hidden" name="isSignIn" value="{{$internalVisitor->isSignIn}}"/>
<input type="hidden" name="tableId" value="{{$internalVisitor->id}}"/>
<button type="submit">
<a >Sign In</a></button>
</form>
@endif
</td>
<td>
@if( $internalVisitor->signIn === 0)
<span>Not SignIn</span>
@elseif ($internalVisitor-> signIn === 1 && $internalVisitor->signOut === 1)
<span>{{ $internalVisitor->signOutTime}}</span>
@elseif($internalVisitor-> signIn === 1 && $internalVisitor->signOut === 0 )
<form method="post" action="{{route('signOutUpdate','id')}}">
{{ csrf_field()}}
<input type="hidden" name="signOut" value="{{$internalVisitor->signOut}}"/>
<input type="hidden" name="tableId" value="{{$internalVisitor->id}}"/>
<button type="submit">
</form>
<a >Sign Out</a></button>
@endif
</td>
<td class=""><a class="btn btn-raised btn-primary btn-sm" href="{{ route('iVisitorEdit',
$internalVisitor->id) }}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
</td>
<td>
<form method="POST" id="delete-form-{{ $internalVisitor->id }}" action="{{ route('iVisitorDelete',$internalVisitor->id) }}" style="display: none;">
{{ csrf_field() }}
{{ method_field('delete') }}
</form>
<button onclick="if(confirm('Are you Sure, You went to delete this?')){
event.preventDefault();
document.getElementById('delete-form-{{ $internalVisitor->id }}').submit();
}else{
event.preventDefault();
}" class="btn btn-raised btn-danger btn-sm"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
</td>
</tr>
@endforeach
</tbody>
</table>
控制器
if (request()->has('from') && request()->has('to')) {
$from = request()->get('from')->date('Y-m-d' . ' 00:00:00', time());
$to = request()->get('to')->date('Y-m-d' . ' 00:00:00', time());
$leads = InternalVisitor::whereBetween('signInTime', [$from, $to])->paginate(5);
dd($leads);
}
脚本
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'Y-m-d H:i:s'
});
$(function(){
$("#From").datepicker();
$("#to").datepicker();
});
$('#range').click(function(){
var From = $('#From').val();
var to = $('#to').val();
if(From != '' && to != '')
{
$.ajax({
url: '{{route('internalVisitor')}}',
method:"POST",
data:{From:From, to:to},
success:function(data)
{
$('#sample_3').html(data);
}
});
}
else
{
alert("Please Select the Date");
}
});
});
</script>
答案 0 :(得分:0)
您需要将from-to
范围分为两个单独的where
条件:
use Carbon\Carbon;
...
$query = InternalVisitor::query();
if (request()->has('from')) {
$from = Carbon::parse(request()->get('from'))->format('Y-m-d') . ' 00:00:00';
$query->where('signInTime', '>=', $from);
}
if (request()->has('to')) {
$to = Carbon::parse(request()->get('to'))->format('Y-m-d') . ' 23:59:59';
$query->where('signInTime', '<=', $from);
}
$leads = $query->paginate(5);