Laravel - 具有 Ajax returns 500 内部服务器错误状态的 daterangepicker

Laravel - daterangepicker with Ajax returns status of 500 internal server error

所以我在我的 Laravel 项目上添加了带 Ajax 的 daterangepicker,在我的 blade 上我有像日历一样的 daterangepicker 来选择日期并刷新 filter.Now我单击刷新它刷新我当前选择的日期但是当我什么都不过滤时 happens.When 我在我的 Safari 浏览器上检查元素它显示:无法加载资源:服务器响应状态为 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>

这是我的 daterange

路线
Route::get('/daterange', 'DateRangeController@index');
Route::post('/daterange/fetch_data', 'DateRangeController@fetch_data')->name('daterange.fetch_data');

这个问题有什么解决办法吗?它需要显示所选日期

的帖子

我将其全部修复为 created_at,而不是日期,现在我的 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);
     }
    }
}

?>

它 returns 我现在所有的帖子,但它喜欢未定义,并且在网络上 url 请求它 returns 我是真实的,而不是未定义的。有什么解决办法吗?

你的成功功能问题, 你试试 handle 属性 not found, 这是完整的脚本标签


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

删除存储库, 祝你的项目好运:)