gpt4 book ai didi

mysql - 获取具有关系的嵌套 json 数据 Laravel Eloquent 模型

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:23 24 4
gpt4 key购买 nike

我正在使用 laravel 制作社交网络,我想在具有嵌套关系的单个数组中显示“'post''comments''comment_by'用户信息

这是我的类和数据库结构

表名字段

Members

         ID => primary key,
name,
email

Posts

         ID => primary key,
postText
fromUserId => foreign key (Members-id)

Comments

         commentText ,
onPostId = > foreign key (Post-id)
fromUserId = > foreign key (Members-id)

Eloquent 模型

  1. 成员(member).php

    类成员扩展模型{//

  2. Post.php

    类帖子扩展模型{ // 公共(public) $timestamps = true;

    函数 getUserDetails(){ 返回 $this->belongsTo('App\Member', 'fromUserId', 'id');

    函数 getCommentDetails(){ 返回 $this->hasMany('App\comment', 'onPostId', 'id');

3.Comment.php

class comment extends Model
{


}

调用获取数组

 $posts=post::with('getUserDetails','getCommentDetails')->get();

*预期输出

{  
"id":1,
"postType":1,
"postText":"my name is parth",
"url":null,
"likesCount":0,
"unlikesCount":0,
"shareCount":0,
"commentsCount":0,
"thumbUrl":null,
"accessMode":1,
"fromUserId":1,
"isAdult":1,
"created_at":null,
"updated_at":null,
"get_user_details":{
"id":1,
"name":"parth",
"email":"parthbhatti95@gmail.com",
"password":"parth123456",
"remember_token":"e1b28a30ab467c52924df64034c386d4",
"created_at":null,
"updated_at":null
},
"get_comment_details":[
{
"id":1,
"commentsText":"dccd",
"onPostId":1,
"fromUserId":1,
"created_at":"2017-05-25 16:44:51",
"updated_at":null
"commented_by":{
"id":1,
"name":"parth",
"email":"parthbhatti95@gmail.com",
"password":"parth123456",
"remember_token":"e1b28a30ab467c52924df64034c386d4",
"created_at":null,
"updated_at":null
},
},
{
"id":3,
"commentsText":"second comment",
"onPostId":1,
"fromUserId":1,
"created_at":"2017-05-26 09:40:51",
"updated_at":null
"commented_by":{
"id":1,
"name":"parth",
"email":"parthbhatti95@gmail.com",
"password":"parth123456",
"remember_token":"e1b28a30ab467c52924df64034c386d4",
"created_at":null,
"updated_at":null
},
},
{
"id":4,
"commentsText":"second comment",
"onPostId":1,
"fromUserId":1,
"created_at":"2017-05-26 09:41:16",
"updated_at":null
"commented_by":{
"id":1,
"name":"parth",
"email":"parthbhatti95@gmail.com",
"password":"parth123456",
"remember_token":"e1b28a30ab467c52924df64034c386d4",
"created_at":null,
"updated_at":null
},
},
{
"id":5,
"commentsText":"third one",
"onPostId":1,
"fromUserId":1,
"created_at":"2017-05-26 09:41:43",
"updated_at":null
"commented_by":{
"id":1,
"name":"parth",
"email":"parthbhatti95@gmail.com",
"password":"parth123456",
"remember_token":"e1b28a30ab467c52924df64034c386d4",
"created_at":null,
"updated_at":null
},
}
]
}

最佳答案

根据您的评论,只需将 commentedBy 关系添加到您的 Member 模型并预先加载它。

在评论模型中。

public function commentedBy()
{
return $this->belongsTo('App\Member', 'fromUserId', 'id');
}

然后像这样急切地加载关系。

$posts = post::with('getUserDetails','getCommentDetails.commentedBy')->get();

关于mysql - 获取具有关系的嵌套 json 数据 Laravel Eloquent 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203202/

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