How to get a Builder object from lines associated with a pivot point - Laravel

I try to get all the “books” that one user has: but I can’t do it the way I need.

I am using the following code:

/*Gets all books from the user whose id is 1*/     
$books= User::find(1)->books();

This returns me an object Collection; but I need an object Builder, as I understand it, when I use the "select" method.

/* This code return me a "Builder" object */
Books::select(array('id', 'name', 'type'));

I need Builderinstead Collectionbecause I use Bllim / Datatables in my project and this package just accepts an object Builder...

If I send it Collection, it will throw me the following 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
   }
}

Does anyone know a solution?

EDIT:

When I use 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}}

, , "select", Datatables ...

:

Books::select(array('id', 'name', 'type'));

, , :

$user = User::find(1);
$user->books()->getQuery()->getQuery();
+1
1

books():

$user = User::find(1);

$user->books(); // relation object
$user->books; // dynamic property

books() , Eloquenr\Builder Query Builder.

books - $user->relations['books'] .


- Query\Builder, columns, getQuery :

$user->books()
   ->getQuery() // get underlying Eloquent\Builder
   ->getQuery() // get underlying Query\Builder
   ->columns    // public property on the above
+2

Source: https://habr.com/ru/post/1538246/


All Articles