我正在构建一个简单的假期管理系统,该系统允许用户执行CRUD操作并根据日期范围检索数据。但是我的 CRUD 操作正在按预期工作。但我无法根据用户从 ajax 调用中选择的日期范围检索数据。这也是第一个使用 laravel 框架和 ajax 开发的应用程序。我希望这里有人可以找出我的代码出现问题的原因,并能够帮助我。
index.blade.php
<div class="input-group input-daterange">
<input type="text" name="from_date" id="from_date" readonly class="form-control">
<div class="input-group-addon to-text"> to </div>
<input type="text" name="to_date" id="to_date" readonly class="form-control">
</div>
HolidayController.php中的ajax请求处理程序方法
public function fetch_data(Request $request)
{
if($request->ajax()) {
if($request->from_date != '' && $request->to_date != '') {
$data = DB::table('holidays')
->whereBetween('startdate',
array($request->from_date, $request->to_date))
->get();
}
else {
$data = DB::table('holidays')->orderBy('startdate', 'desc')
->get();
}
echo json_encode($data);
}
}
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::Resource('holiday', 'HolidayController');
从custom_script.js发出ajax请求
$(document).ready(function() {
var date = new Date();
$('.input-daterange').datepicker( {
todayBtn: 'linked',
format: 'yyyy-mm-dd',
autoclose: true
});
var _token = $('input[name="_token"]').val();
fetch_data();
function fetch_data(from_date = '', to_date = '') {
$.ajax({
url:"{{ route('holiday.fetch_data')}}",
method:"POST",
data:{
from_date:from_date, to_date:to_date, _token:_token
},
dataType:"json",
success:function(data) {
var output = '';
$('#total_records').text(data.length);
for(var count = 0; count < data.length; count++) {
output += '<tr>';
output += '<td>' + data[count].id + '</td>';
output += '<td>' + data[count].firstname + '</td>';
output += '<td>' + data[count].lastname + '</td>';
output += '<td>' + data[count].startdate + '</td>';
output += '<td>' + data[count].enddate + '</td></tr>';
}
$('tbody').html(output);
}
})
}
$('#filter').click(function() {
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '') {
fetch_data(from_date, to_date);
}
else {
alert('Both Date is required');
}
});
$('#refresh').click(function() {
$('#from_date').val('');
$('#to_date').val('');
fetch_data();
});
});
最佳答案
查询
没有问题,除非$request->ajax()
可能暂时删除。并按如下方式更改 Blade 文件,输入
类型应为日期
,而不是文本
并删除readonly
属性。
<input type="date" name="from_date" id="from_date" class="form-control">
<div class="input-group-addon to-text"> to </div>
<input type="date" name="to_date" id="to_date" class="form-control">
并确保日期
数据是数据库中的有效数据
用于演示
$(document).ready(function() {
function fetch_data(from_date = '', to_date = '') {
if(from_date != '' && to_date != '') {
console.log(from_date+' | '+to_date)
}
}
$('#filter').click(function() {
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '') {
fetch_data(from_date, to_date);
}
else {
alert('Both Date is required');
}
});
$('#refresh').click(function() {
$('#from_date').val('');
$('#to_date').val('');
fetch_data();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-group input-daterange">
<input type="date" name="from_date" id="from_date" class="form-control">
<div class="input-group-addon to-text"> to </div>
<input type="date" name="to_date" id="to_date" class="form-control">
<button id="filter">filter</button>
<button id="refresh">refresh</button>
</div>
关于php - 如何在laravel php中使用ajax根据日期范围从mysql数据库检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59693311/