I'm trying to get all the "books" that one user have: but I can't do it how I need.
I use the following code:
/*Gets all books from the user whose id is 1*/
$books= User::find(1)->books();
That return to me an Collection
object; but I need a Builder
object, as I get when I use the "select" method.
/* This code return me a "Builder" object */
Books::select(array('id', 'name', 'type'));
I need the Builder
instead of Collection
because I using Bllim/Datatables on my project and this package just accept a Builder
object...
If I send it a Collection
its throw me the next error (500 - Internal Server Error):
{
"error":
{
"type":"ErrorException",
"message":"Undefined property: Illuminate\\Database\\Eloquent\\Builder::$columns",
"file":"\/var\/www\/proyect\/myproyect\/vendor\/bllim\/datatables\/src\/Bllim\/Datatables\/Datatables.php",
"line":256
}
}
Anybody knows the solution?
EDIT:
When I use the getQuery() method twice I get the following error:
{"error":{"type":"ErrorException","message":"array_values() expects parameter 1 to be array, null given","file":"\/var\/www\/proyect\/myproyect\/vendor\/bllim\/datatables\/src\/Bllim\/Datatables\/Datatables.php","line":550}}
Is rare, because when I used the "select" method Datatables worked perfectly...
This code works:
Books::select(array('id', 'name', 'type'));
But the code you told me doesn't work:
$user = User::find(1);
$user->books()->getQuery()->getQuery();