我已经建立了一个报告系统,用户可以在其中报告违反站点规则的帖子或评论。
我想同时查询评论和发布报告,并将报告归类到属于它们的评论或发布中。然后,我想按帖子或评论created_at
字段进行整理。
帖子
评论
报告
用户
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天前发布的信息
这是3天前发布的信息
2。。获取所有包含报告的评论,创建报告的用户,评论的发布者,并按最新顺序整理。
$comments = Comment::whereHas('reports')->with('reports.user', 'post')->latest()->get();
这是1天前发布的信息
用户10对帖子的讨厌评论
这是3天前的帖子
用户15对帖子的另一种讨厌评论
3。。这是我遇到麻烦的地方。我想将Post
和Comment
报告组织在一起,并按照上述模型将报告分组。示例:
这是1天前发布的信息
这是1天前发布的信息
用户10对帖子的讨厌评论
这是3天前发布的信息
这是3天前的帖子
用户15对帖子的另一种讨厌评论
这是我尝试将Post
和Comment
的报告汇总在一起的方法:
$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"
}
}
]
我遇到的问题是在Comment
和Post
created_at
日期之前格式化报告权限,分页并组织报告。有没有一种方法可以只查询Post
,Comments
,User
和Reports
的模型,共同实现我想做的事情?
我真的很抱歉这么长的帖子。
答案 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>
);
}
}