Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bllim DataTables in Laravel 4 throws undefined getQuery() exception

I have the following simple controller:

class OrdersController extends \BaseController {

    public function index()
    {
        $orders = Order::all();

        return Datatables::of($orders)->make();
    }
}

Trying to use the bllim DataTables package to output my tables. When I can DataTables above, I get this error:

Call to undefined method Illuminate\Database\Eloquent\Collection::getQuery()

The error is located in \Bllim\Datatables\Datatables.php on the line:

$this->columns = $this->query_type == 'eloquent' ? $this->query->getQuery()->columns : $this->query->columns;

This method should be defined, unless I am mistaken. So what is missing here?

like image 985
eComEvo Avatar asked Apr 27 '14 05:04

eComEvo


1 Answers

Usage

It is very simple to use this bundle. Just create your own fluent query object or eloquent object without getting results (that means don't use get(), all() or similar methods) and give it to Datatables. You are free to use all Eloquent ORM and Fluent Query Builder features.

By calling the all() method you are returning an Illuminate\Database\Eloquent\Collection object which in this case doesn't contain the getQuery() method, you need to pass a Illuminate\Database\Eloquent\Builder or Illuminate\Database\Query\Builder instead.

Try this:

return Datatables::of(Order::select(array('id', 'othercolumns')))->make();

Or this:

$query = DB::table('orders')->select(array('id','othercolumns'));
return Datatables::of($query)->make();

Select the columns you want to show in the data table in the array.

like image 101
The Alpha Avatar answered Sep 27 '22 16:09

The Alpha