将共享多态关系的两个模型合并或组合在一起?

时间:2018-11-27 01:57:04

标签: laravel eloquent

我已经建立了一个报告系统,用户可以在其中报告违反站点规则的帖子或评论。

我想同时查询评论和发布报告,并将报告归类到属于它们的评论或发布中。然后,我想按帖子或评论created_at字段进行整理。

表格

帖子

  • id(int)
  • 内容(文字)
  • 标题(文本)
  • created_at

评论

  • id(int)
  • 内容(int)
  • post_id(int)
  • created_at

报告

  • id(int)
  • user_id(int)
  • 内容(文本)
  • reportable_id(int)
  • reportable_type(字符串)

用户

  • id(int)
  • 名称(字符串)

关系

Post.php

public function reports()
{
    return $this->morphMany('App\Report', 'reportable');
}

Comment.php

public function reports()
{
    return $this->morphMany('App\Report', 'reportable');
}

Report.php

public function reportable()
{
    return $this->morphTo();
}

public function user()
{
    return $this->belongsTo(User::class, 'user_id');
}

我想做什么

我想用3种不同的方式来组织报告。

1。。最晚获取所有包含报告以及撰写报告的用户的帖子,并按最新顺序进行整理。

$posts = Post::whereHas('reports')->with('reports.user', 'comments')->latest()->get();

输出:

这是1天前发布的信息

  • 报告1用户1的评论
  • 关于用户4的评论的报告2

这是3天前发布的信息

  • 用户3的报告1
  • 用户5的报告2

2。。获取所有包含报告的评论,创建报告的用户,评论的发布者,并按最新顺序整理。

$comments  = Comment::whereHas('reports')->with('reports.user', 'post')->latest()->get();

这是1天前发布的信息

用户10对帖子的讨厌评论

  • 用户1的报告1
  • 用户4的报告2

这是3天前的帖子

用户15对帖子的另一种讨厌评论

  • 用户3的报告1
  • 用户5的报告2

3。。这是我遇到麻烦的地方。我想将PostComment报告组织在一起,并按照上述模型将报告分组。示例:

这是1天前发布的信息

  • 报告1用户1的评论
  • 关于用户4的评论的报告2

这是1天前发布的信息

用户10对帖子的讨厌评论

  • 用户1的报告1
  • 用户4的报告2

这是3天前发布的信息

  • 用户3的报告1
  • 用户5的报告2

这是3天前的帖子

用户15对帖子的另一种讨厌评论

  • 用户3的报告1
  • 用户5的报告2

这是我尝试将PostComment的报告汇总在一起的方法:

$reports = Report::with('reportable')->latest()->get();
//return view('reports.index', compact('posts'));

输出

"7": [
    {
        "id": 9,
        "user_id": 2,
        "content": "Guy is being a jerk",
        "reportable_id": 7,
        "reportable_type": "App\\Post",
        "created_at": "2017-08-22 01:31:25",
        "updated_at": "2017-08-22 01:31:25",
        "is_handled": 0,
        "reportable": {
            "id": 7,
            "author_id": 1,
            "category_id": null,
            "title": "A forum post!",
            "created_at": "2017-08-08 23:03:22",
            "updated_at": "2017-08-08 23:03:22",
        }
    },
    {
        "id": 8,
        "user_id": 2,
        "content": "this guy is being rude",
        "reportable_id": 7,
        "reportable_type": "App\\Post",
        "created_at": "2017-08-22 01:31:02",
        "updated_at": "2017-08-22 01:31:02",
        "is_handled": 0,
        "reportable": {
            "id": 7,
            "author_id": 1,
            "category_id": null,
            "title": "A forum post!",
            "created_at": "2017-08-08 23:03:22",
            "updated_at": "2017-08-08 23:03:22",
        }
    },
    {
        "id": 6,
        "user_id": 1,
        "content": "Report number 1",
        "reportable_id": 7,
        "reportable_type": "App\\Post",
        "created_at": "2017-08-21 20:40:55",
        "updated_at": "2017-08-21 20:40:55",
        "is_handled": 0,
        "reportable": {
            "id": 7,
            "author_id": 1,
            "title": "A forum post!",
            "created_at": "2017-08-08 23:03:22",
            "updated_at": "2017-08-08 23:03:22"
        }
    }
],
"9": [
    {
        "id": 10,
        "user_id": 4,
        "content": "Rule 1",
        "reportable_id": 9,
        "reportable_type": "App\\Post",
        "created_at": "2017-09-20 21:31:50",
        "updated_at": "2017-09-20 21:31:50",
        "is_handled": 0,
        "reportable": {
            "id": 9,
            "author_id": 1,
            "title": "Hello world!",
            "created_at": "2017-09-10 16:49:07",
            "updated_at": "2017-09-10 16:49:07",
        }
    }
],
"24": [
    {
        "id": 11,
        "user_id": 4,
        "content": "Rule 2",
        "reportable_id": 24,
        "reportable_type": "App\\Comment",
        "created_at": "2017-09-20 22:28:15",
        "updated_at": "2017-09-21 03:03:28",
        "is_handled": 1,
        "reportable": {
            "id": 24,
            "user_id": 1,
            "content": "<p>A comment to be edited</p>",
            "created_at": "2017-09-17 22:41:35",
            "updated_at": "2017-09-17 22:41:35",
            "commentable_id": 9,
            "commentable_type": "App\\Forum"
        }
    }
]

我遇到的问题是在CommentPost created_at日期之前格式化报告权限,分页并组织报告。有没有一种方法可以只查询PostCommentsUserReports的模型,共同实现我想做的事情?

我真的很抱歉这么长的帖子。

1 个答案:

答案 0 :(得分:-2)

格式化此格式的另一种方法是使用hasMany()代替export default class App extends Component { constructor() { super(); this.state = { text: "Hello " }; this.handleChange = this.handleChange.bind(this); } onPress = () => { console.log("current ==> ", this.state); alert(this.state.text); }; handleChange = typedText => { console.log("update => ", typedText); this.setState( { text: typedText } ); }; render() { return ( <View style={styles.container}> <Text>{this.state.value}</Text> <Button onPress={this.onPress} title="Click Me " /> <TextInput placeholder="Type your name here" onChangeText={this.handleChange} /> </View> ); } }