7

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?

eComEvo
  • 11,669
  • 26
  • 89
  • 145

1 Answers1

13

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.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 1
    Totally missed that. **face palm** Thanks! – eComEvo Apr 27 '14 at 19:46
  • It's kind of super natural @eComEvo :-) – The Alpha Apr 28 '14 at 18:43
  • @TheAlpha Seems you know a lot of Laravel Relationships. May you see my question? I can't find a solution. I have a similar problem to this. http://stackoverflow.com/questions/27447389/how-to-get-a-builder-object-from-rows-related-to-pivot-laravel – MartaGom Dec 12 '14 at 17:26