php – 在Laravel中创建并返回一个具有关系的模型
发布时间:2020-12-14 19:35:22 所属栏目:大数据 来源:网络整理
导读:我有几个端点可以在此应用程序中获取/发布注释. GET端点使用Eloquent的方法将响应的作者数据包含在响应中: public function getComments(Note $note) { return $note-comments()-with(['author'])-get();} 如何在创建/返回Eloquent模型时包含作者数据?这是
|
我有几个端点可以在此应用程序中获取/发布注释.
GET端点使用Eloquent的方法将响应的作者数据包含在响应中: public function getComments(Note $note) {
return $note->comments()->with(['author'])->get();
}
如何在创建/返回Eloquent模型时包含作者数据?这是我目前的方法: public function postComment(Note $note,Request $request) {
$user = $this->appUserRepo->getFromRequest($request);
$text = $request->text;
$comment = $note->comments()->create([
'app_user_id' => $user->id,'text' => $text
]);
return $comment;
}
我正在寻找这样的东西(但这不起作用): public function postComment(Note $note,'text' => $text
]);
return $comment->with(['author']);
}
解决方法
你应该尝试使用:
$comment->load('author');
return $comment;
加载作者关系以进行评论. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
