gpt4 book ai didi

jquery - Laravel 5.6 中的日期时间范围搜索

转载 作者:行者123 更新时间:2023-11-28 02:29:10 35 4
gpt4 key购买 nike

我在表格中显示我的数据。我的故事中有一个签到时间。现在我想过滤两个日期之间的数据。我从日期和到日期添加了 jquery 日期时间选择器。但是我很困惑只将日期范围数据显示到同一个表中,默认情况下显示所有数据。任何人都可以建议我使用模型 View 和日期范围过滤器 Controller 的确切方法吗?

日期范围输入

<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>

Controller

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>

最佳答案

您需要将您的 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);

关于jquery - Laravel 5.6 中的日期时间范围搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52034518/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com