如何使用laravel,mongo和datatables对多个列进行排序?

时间:2017-06-24 17:32:14

标签: laravel laravel-5 datatables laravel-5.4

我在laravel有一个项目。数据库是mongodb。我正在使用包来连接laravel和mongo“https://github.com/jenssegers/laravel-mongodb”。在我的项目中,我有一个预订列表页面。 jQuery Datatables服务器端功能用于分页,搜索和排序。目前的分页和搜索工作正在进行,但排序不起作用。我收到一些错误,请检查附件。我认为这是查询中排序部分的问题。请检查以下代码并帮助我。感谢。

bookings.blade.php

$('#booking_data').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax": {
                    "url": '{{ route('bookings.datatables') }}',
                    "dataType": "json",
                    "type": "POST",
                    "data":{ _token: "{{csrf_token()}}"}
                },
                "columns": [
                    { "data": "hash" },
                    { "data": "invoice_number" },
                    { "data": "usrEmail" },
                    { "data": "checkin_from" },
                    { "data": "reserve_to" },
                    { "data": "beds" },
                    { "data": "dormitory" },
                    { "data": "sleeps" },
                    { "data": "status" },
                    { "data": "payment_status" },
                    { "data": "payment_type" },
                    { "data": "total_prepayment_amount" },
                    { "data": "txid" }
                ]
            });

BookingController.php

public function dataTables(Request $request)
    {
        $columns       = array('invoice_number', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid');

        $totalData     = Booking::where('is_delete', 0)->count();

        $totalFiltered = $totalData;
        $limit         = (int)$request->input('length');
        $start         = (int)$request->input('start');
        $order         = $columns[$request->input('order.0.column')];
        $dir           = $request->input('order.0.dir');

        if(empty($request->input('search.value')))
        {
            $bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
                ->where('is_delete', 0)
                ->skip($start)
                ->take($limit)
                ->orderBy($order, $dir)
                ->get();
        }
        else {
            $search   = $request->input('search.value');
            $bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
                ->where('is_delete', 0)
                ->where(function($query) use ($search) { 
                    $query->where('invoice_number', 'like', "%{$search}%")
                        ->orWhere('payment_type', 'like', "%{$search}%");
                })
                ->skip($start)
                ->take($limit)
                ->orderBy($order, $dir)
                ->get();

            $totalFiltered = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
                ->where('is_delete', 0)
                ->where(function($query) use ($search) {
                    $query->where('invoice_number', 'like', "%{$search}%")
                        ->orWhere('payment_type', 'like', "%{$search}%");
                })
                ->count();
        }

        $data = array();
        if(!empty($bookings))
        {
            foreach ($bookings as $key=> $booking)
            {
                $nestedData['hash']                    = '<input type="checkbox" name="id[]" value="'.$booking->_id.'" />';
                $nestedData['invoice_number']          = '<a class="nounderline modalBooking" data-toggle="modal" data-target="#bookingModal_'.$booking->_id.'" data-modalID="'.$booking->_id.'">'.$booking->invoice_number.'</a>';
                $nestedData['usrEmail']                = $booking->user;
                $nestedData['checkin_from']            = ($booking->checkin_from)->format('d.m.y');
                $nestedData['reserve_to']              = ($booking->reserve_to)->format('d.m.y');
                $nestedData['beds']                    = $booking->beds;
                $nestedData['dormitory']               = $booking->dormitory;
                $nestedData['sleeps']                  = $booking->sleeps;
                $nestedData['status']                  = $booking->status;
                $nestedData['payment_status']          = $booking->payment_status;
                $nestedData['payment_type']            = $booking->payment_type;
                $nestedData['total_prepayment_amount'] = $booking->total_prepayment_amount;
                $nestedData['txid']                    = $booking->txid;
                $data[]                                = $nestedData;
            }
        }

        $json_data = array(
            'draw'            => (int)$request->input('draw'),
            'recordsTotal'    => (int)$totalData,
            'recordsFiltered' => (int)$totalFiltered,
            'data'            => $data
        );

        echo json_encode($json_data);
}

please check the image attached

1 个答案:

答案 0 :(得分:0)

问题在于BookingController

$columns       = array('invoice_number', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid');
...
$order         = $columns[$request->input('order.0.column')];

DataTable会在请求中发送列名,而不是key,因此您无法以这种方式使用它。也许你可以用这个代替:

$order = $request->input('order.0.column');

如果要对有效列进行验证,可以使用in_array()函数。