所以我在Laravel项目中用Ajax添加了daterangepicker,在刀片上我有一个daterangepicker像一个日历来选择日期并刷新和过滤。现在,当我单击刷新时它会刷新我当前选择的日期,但是当我过滤时什么也没有发生当我在Safari浏览器中检查Element时,向我显示:无法加载资源:服务器的响应状态为500(内部服务器错误)。
这是我的DateRangeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Post;
use Auth;
class DateRangeController extends Controller
{
public function index()
{
$posts = Post::where('user_id', Auth::id());
return view('daterange')->with('posts', $posts);
}
public function fetch_data(Request $request)
{
if($request->ajax())
{
if($request->from_date != '' && $request->to_date != '')
{
$data = DB::table('posts')
->whereBetween('date', array($request->from_date, $request->to_date))
->get();
}
else
{
$data = DB::table('posts')->orderBy('date', 'desc')->get();
}
echo json_encode($data);
}
}
}
?>
这是我的daterange.blade.php,带有脚本
<!DOCTYPE html>
<html>
<head>
<title>Date Range Fiter Data in Laravel using Ajax</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.js"></script>
</head>
<body>
<br />
<div class="container box">
<h3 align="center">Date Range Fiter Data in Laravel using Ajax</h3><br />
<div class="panel panel-default">
<div class="panel-heading">
<div class="row">
<div class="col-md-5">Sample Data - Total Records - <b><span id="total_records"></span></b></div>
<div class="col-md-5">
<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</div>
<input type="text" name="to_date" id="to_date" readonly class="form-control" />
</div>
</div>
<div class="col-md-2">
<button type="button" name="filter" id="filter" class="btn btn-info btn-sm">Filter</button>
<button type="button" name="refresh" id="refresh" class="btn btn-warning btn-sm">Refresh</button>
</div>
</div>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th width="35%">Broj Kesice</th>
<th width="50%">Ime</th>
<th width="15%">Broj Telefona</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
<tr>
<td>{{$post->br_kesice}}</td>
<td>{{$post->ime}}</td>
<td>{{$post->br_telefona}}</td>
</tr>
@endforeach
</tbody>
</table>
{{ csrf_field() }}
</div>
</div>
</div>
</div>
</body>
</html>
<script>
$(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('daterange.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].post_br_kesice + '</td>';
output += '<td>' + data[count].post_ime + '</td>';
output += '<td>' + data[count].date + '</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();
});
});
</script>
这是我到达日期范围的路线
Route::get('/daterange', 'DateRangeController@index');
Route::post('/daterange/fetch_data', 'DateRangeController@fetch_data')->name('daterange.fetch_data');
任何解决此问题的方法?它需要向我显示选定日期的帖子
答案 0 :(得分:0)
我将其全部固定为created_at,而不是date,现在我的DateRangeController如下所示:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Post;
use Auth;
class DateRangeController extends Controller
{
public function index()
{
$posts = Post::where('user_id', Auth::id());
return view('daterange')->with('posts', $posts);
}
public function fetch_data(Request $request)
{
if($request->ajax())
{
if($request->from_created_at != '' && $request->to_created_at != '')
{
$data = DB::table('posts')
->whereBetween('created_at', array($request->from_created_at, $request->to_created_at))
->get();
}
else
{
$data = DB::table('posts')->orderBy('created_at', 'desc')->get();
}
return json_encode($data);
}
}
}
?>
它现在向我返回所有帖子,但是它像未定义的一样,并且在网络url请求上,它向我返回真实的,而不是未定义的。有解决方案吗?
答案 1 :(得分:0)
您的成功功能问题, 您尝试处理找不到的属性, 这是完整的脚本标记
<script>
var _token = $('input[name="_token"]').val();
$(document).ready(function(){
var date = new Date();
$('.input-daterange').datepicker({
todayBtn: 'linked',
format: 'yyyy-mm-dd',
autoclose: true
});
//if you leave this ,will load the data if you remove this remove comment from line number 43 to 49 to load data :)
fetch_data();
$('#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();
});
});
function fetch_data(from_date = '', to_date = '')
{
$.ajax({
url:"{{ route('daterange.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);
$.each(data,function (key,value) {
console.log(value)
output += '<tr>';
output += '<td>' + value.br_kesice + '</td>';
output += '<td>' + value.ime + '</td>';
output += '<td>' + value.created_at + '</td></tr>';
});
$('tbody').html(output);
}
})
}
</script>
删除存储库, 祝您项目顺利:)