如何在两个日期之间过滤Laravel Datatable?

时间:2017-11-30 09:45:22

标签: jquery laravel datatables

我使用https://datatables.yajrabox.com/fluent/advance-filter

但我得到了这个错误。

filterByDate()用于使用" startdate'来获取数据。并且' enddate'变量

这是我的代码。

<script type="text/javascript">
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
        }
    });

    $(function() {
        $('#postTable').DataTable({
            processing: true,
            serverSide: true,
            ajax:{
                url:'{{route('filterbydate')}}',
                data: {
                    '_token': $('input[name=_token]').val(),
                    'starttime': $('#startdate_edit').val(),
                    'endtime': $('#enddate_edit').val(),
                },
            } ,
            columns: [
                {data:'id',name:'id'},
                {data: 'AccountId', name: 'AccountId'},
                {data: 'EnterTime', name: 'EnterTime'},
                {data: 'ExitTime', name: 'ExitTime'},
                {data: 'StartMoney', name: 'StartMoney'},
                {data: 'EndMoney', name: 'EndMoney'},
                {data: 'UserBenefitRate', name: 'UserBenefitRate'},

            ],
        });
    });

</script>

    public function filterByDate(Request $request)
    {

        $startdata = ['start_at'  => $request->get('starttime')];
        $enddata = ['start_at'  => $request->get('endtime')];

        $rules = ['start_at' => 'date_format:Y-m-d'];
        $validator1 = \Validator::make($startdata, $rules);
        $validator2 = \Validator::make($enddata, $rules);

        if ($validator1->fails()||$validator2->fails()) {
            return Response::json(array('errors' =>  'The date is required'));
        }
        else
        {
            $model = AccountState::where([['EnterTime', '>=', $request->starttime],['ExitTime','<=', $request->endtime]])->get();
            return response()->json($model);
        }

    }

我没有使用&#39;长度&#39;但我得到了这个错误。

jquery.dataTables.min.js:38未捕获的TypeError:无法读取属性

&#39;长度&#39;未定义的         在vb(jquery.dataTables.min.js:38)         在jquery.dataTables.min.js:35

请帮帮我。

1 个答案:

答案 0 :(得分:0)

这就是答案。

   $logs = DB::table('userlog') -> select(DB::raw('sum(win) as win'), DB::raw('sum(bet) as bet'),DB::raw('DATE(starttime) as time'))
    ->where(DB::raw('DATE(starttime)'),'>=',$starttime)->where(DB::raw('DATE(starttime)'),'<=',$finishtime)->whereIn('acid',$userids)->groupBy(DB::raw('DATE(starttime)'))->get(); 
    foreach($logs as $log)
    {
         $win = $log->bet- $log->win;
         $post = array("win" => $win, "time" => $log->time);
         array_push($posts, $post);
    }


    return view('hometable',['posts'=>$posts]);