gpt4 book ai didi

php - 拉拉维尔 5.3 : Many to many realtionship's data

转载 作者:行者123 更新时间:2023-11-29 19:30:35 25 4
gpt4 key购买 nike

我在用户项目之间有多对多关系

关系名称为:User 模型有 projectsProject 模型有 users。并且格式正确。

我对数据表使用以下代码:

public function handleProjects(Request $request)
{
$responseData = \App\Misc::handleDatatable([
"request" => $request,
"model" => "\App\Project",
"actions" => [
"delete" => function($records) {
foreach($records as $record) {
$record->delete();
}
return [
"status" => "OK",
"message" => "Successfully deleted the selected product types!",
];
},
],
"columns" => [
"title" => "title",
"users.username" => "users.username",
"level" => "level",
"users.username" => "users.username",
"created_at" => function($value) {
return $value->diffForHumans();
}
],
]);
return response()->json($responseData);
}

请注意"users.username"=> "users.username",最后第8行,如何从users表中获取用户名?

上面提到了关系名称。

它给了我以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.username' in 'field list' (SQL: SELECT projects.*, categories.name AS b74HW, subcategory.name AS rLTng, users.username AS fUfHg from `projects` left join `categories` as `categories` on `categories`.`id` = `projects`.`categories_id` left join `sub_category` as `subcategory` on `subcategory`.`id` = `projects`.`subcategory_id` group by `id` order by `projects`.`id` desc limit 15 offset 0) 

最佳答案

这个错误非常直接。您没有在查询中使用联接,因此假设您有一个名为 users.username 的列,并且在 users 表中有一个名为 username 的列。

你如何称呼这种关系?像这样的东西:

$data = Users::find(1)->with('projects')->get();
//这个用户的id为1,并立即加载关系。

另外不要忘记循环遍历结果,例如:

foreach($data->projects as $project) {
...
}

关于php - 拉拉维尔 5.3 : Many to many realtionship's data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41803903/

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