gpt4 book ai didi

php - 如何获取包含与 Bllim 数据表的另一个表相关的行的数组 - Laravel

转载 作者:可可西里 更新时间:2023-11-01 13:27:59 25 4
gpt4 key购买 nike

我将 Bllim/Datatables 包用于我的网络应用程序中的数据表;但我无法检索所有相关行。

我使用下面的代码:

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

然后我将它发送到 Datatables::of 方法如下:

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();

但是所有这些都会返回内部服务器错误 (500),并显示以下消息:

{"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
}
}

我的数据库结构:

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;

views 我有下一个:

/*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");
}

有人知道我需要的 make 方法吗?

最佳答案

你需要用下一句:

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

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

您还可以使用 where 从句:

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

请注意,我使用了两次 getQuery(),这是因为 Bllim/Datatables 需要一个 Query/Builder 对象。

关于php - 如何获取包含与 Bllim 数据表的另一个表相关的行的数组 - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com