数据表::的() - >使(真);返回错误调用数组上的成员函数getQuery()

时间:2018-03-19 06:34:07

标签: datatables laravel-5.2 yajra-datatable

美好的一天。我想在数据表中显示我的数据,但我遇到了错误并在数组上调用了成员函数getQuery()。

Iámnew到laravel和这个yajra图书馆。希望有人可以帮我解决这个错误。

这是我的ajax。它会将level_id和level传递给我的控制器,然后返回查询结果(如果匹配)。

//GENERAL
var $i=0;
var level_id = '{{ @$level->id }}';
var level = '{{ @$_level }}';

var viewStudent
function view_students($this){
    viewStudent
    if ($('#viewStudent-div').is(':hidden')) {
        $(".sidebar .menus").removeClass("active");
        $($this).addClass("active");

        $('#viewStudent-div').siblings().hide('slow/400/fast', function() {
            $('#viewStudent-div').show('slow/400/fast');
        });
    }
}

$(function(){
    viewStudent = $('#viewStudent-table').DataTable({
        order: [[ 0, "asc" ]],
        Language: {
            sLoadingRecords:"<img src='{{asset('assets/images/load.gif')}}' style='max-height:30px;'/> Fetching data...",
            sEmptyTable: "No data available"
        },
        ajax:{
            url:'{{ route("json_product_user") }}',
            data: function (d) {
                d.level_id = level_id;
                d.level = level;
            }
        },
        columnDefs: [
            {
                targets: 0,
                searchable:false,
                orderable:false,
            },
        ],
        columns:[
            {
                data: "full_name",
                render: function(data, type, row){
                    return data;
                }
            },
            {
                data: "product_name",
                render: function(data, type, row){
                    var html = '';
                    html += '<p style="">'+data+'</p>';
                    return html;
                }
            },
            {
                data: "level",
                render: function(data, type, row){
                    return data;
                }
            },
            {
                data: "date_purchase",
                render: function(data, type, row){
                var html = '';
                html += '<p style="text-align: center">'+moment(data).format('MMM-DD-YYYY')+'</p>';
                    return html;
                }
            }
        ]
    });
});

这是我的控制器

public function viewStudents(Request $request)
{
    if( $request->has('level_id') ){
        if($request->level == 'school') {
            $query = DB::table('product_user')
                ->leftJoin('products', 'product_user.product_id', '=', 'products.id')
                ->leftJoin('school_products', 'product_user.product_id', '=', 'school_products.product_id')
                ->leftJoin('users', 'product_user.user_id', '=', 'users.id')
                ->whereRaw("school_products.school_id = " . $request->level_id . " OR products.organization_id = ".$org_id)
                ->select('products.name as product_name', DB::raw('CONCAT(users.last_name, ", ", users.first_name ) AS  full_name'), 'product_user.created_at', 'products.level as level')
                ->get();
        }
        else{
            $query= DB::table('product_user')
                ->leftJoin('products', 'product_user.product_id', '=', 'products.id')
                ->leftJoin('school_products', 'product_user.product_id', '=', 'school_products.product_id')
                ->leftJoin('users', 'product_user.user_id', '=', 'users.id')
                ->whereRaw("products.level = 'organization' AND products.organization_id = ".$request->level_id)
                ->select('products.name as product_name', DB::raw('CONCAT(users.last_name, ", ", users.first_name ) AS  full_name'), 'product_user.created_at', 'products.level as level')
                ->get();

        }
    }
     //        return $query;
    return  Datatables::of($query)->make(true);
}

1 个答案:

答案 0 :(得分:0)

我相信因为你使用的是DB,所以DB select的返回类型是一个普通对象数组(stdClass)。在这种情况下,您必须使用

将输出转换为\ Illuminate \ Support \ Collection
$collection = new \Illuminate\Support\Collection($query);
return  Datatables::of($collection)->make(true);

$collection = collect($query);
return Datatables::of($collection)->make(true);

collect()在laravel 5.3中为我工作。请看一下这个答案: https://github.com/laravel/docs/issues/2867#issuecomment-343664438