7

Uso il pacchetto Bllim/Datatables per i dati della mia applicazione web; ma non riesco a recuperare tutte le righe correlate.Come ottenere array con righe correlate a un'altra tabella per Bllim Datatable - Laravel

Io uso il codice qui sotto:

$books= $client->books()->where('books.type', 0); 

e trasmetto a DataTable :: metodo come segue:

return Datatables::of($books) 
    ->edit_column('type', '{{$type}}') 
    ->edit_column('created_at', function($obj) { 
    return $obj->created_at->format('d/m/Y (h:i)'); 
}) 
->set_index_column('id') 
->make(); 

Ma tutto questo ritorno un Internal Server Error (500) con il seguire un messaggio:

{"error": 
    { 
    "type":"ErrorException", 
    "message":"Undefined property: Illuminate\\Database\\Eloquent\\Builder::$columns", 
    "file":"\/var\/www\/clients\/myapp\/vendor\/bllim\/datatables\/src\/Bllim\/Datatables\/Datatables.php", 
    "line":256 
    } 
} 

MY DATABASE STRUTTURA:

create table clients(
    id int not null auto_increment, 
    name varchar(10) not null, 
    password varchar(60) not null, 
    unique(name), 
    primary key(id), 
    created_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    updated_at timestamp NOT NULL DEFAULT now() ON UPDATE now() 
)CHARSET utf8 COLLATE utf8_spanish_ci; 

/* PIVOT TABLE */ 
create table book_client(
    id int not null auto_increment, 
    book_id int, 
    client_id int, 
    primary key(id), 
    FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE, 
    FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE CASCADE 
)CHARSET utf8 COLLATE utf8_spanish_ci; 

create table books(
    id int not null auto_increment, 
    name varchar(50) not null, 
    description varchar(500), 
    type varchar(10) not null, 
    primary key(id), 
    created_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    updated_at timestamp NOT NULL DEFAULT now() ON UPDATE now() 
)CHARSET utf8 COLLATE utf8_spanish_ci; 

Negli vista ho il prossimo:

/*In the view of clients*/ 
public function books(){ 
    return $this->belongsToMany("Book"); 
} 
/*In the view of books: (yes, in my case, a book could belong to multiple clients*/ 
public function clients(){ 
    return $this->belongsToMany("Client"); 
} 

Qualcuno conosce il metodo per il trucco ho bisogno?

+0

Se la mia domanda è confusa ... posso riscriverla. – MartaGom

risposta

3

è necessario utilizzare la frase successiva:

/* The client whose ID is 1*/ 
$client = Client::find(1); 

$client->books() 
->getQuery() 
->getQuery() 
->select(array('id', 'ColumnA', 'ColumnB')); 

È possibile utilizzare il where clausule anche:

$client->books() 
->getQuery() 
->getQuery() 
->select(array('id', 'ColumnA', 'ColumnB')) 
->where('id','=',1); 

Nota che ho usato getQuery() due volte, questo è perché Bllim/Datatables bisogno di un oggetto Query/Builder.

+0

Grazie per la tua risposta, ma mi ha generato un errore "Nessun dato" – MartaGom

+1

Non stai mostrando l'errore completo ... Ma immagino che starai usando la clausola 'dove' con una condizione che non restituisce alcuna riga .. . Potrebbe essere? – tomloprod

+0

Mi dispiace, ho scelto il cliente non aveva libri !!! Il tuo codice funziona perfettamente! – MartaGom