我正在尝试获取所有包含报告的帖子,然后急于加载报告。
关系
//Post.php
public function reports()
{
return $this->morphMany('App\Report', 'reportable');
}
//Report.php
public function reportable()
{
return $this->morphTo();
}
报表控制器
public function postReportsIndex()
{
$posts = Post::whereHas('reports')
->with('reports.creator', 'comments')
->latest()
->paginate();
//return view('channels.reports.post-reports', compact('posts'));
return $posts;
}
JSON输出
当我return $post;
并获取JSON输出时,一切正常,帖子reports: []
就在那里。
{
"current_page":1,
"data":[
{
"id":101,
"user_id":1,
"title":"GREATEST SKATEBOARDING TRICKS EVER 2018! #1 BEST SKATE & SKATEBOARD & SKATING TRICKS COMPILATION",
"content":"GREATEST SKATEBOARDING TRICKS EVER 2018! #1 BEST SKATE & SKATEBOARD & SKATING TRICKS COMPILATION. Skateboarding is one of the most popular types of extreme s...",
"created_at":"2018-09-02 22:14:27",
"updated_at":"2018-09-22 06:38:26",
"reports":[
{
"id":1,
"user_id":1,
"content":"Broke rule 1",
"reportable_id":101,
"reportable_type":"App\\Post",
"reportable_created_at":"2018-11-29 20:27:56",
"created_at":"2018-11-29 20:27:47",
"updated_at":null,
"creator":{
"id":1,
"username":"Frank",
"slug":"Frank",
"name":"Frank Lettuce",
"email":"frank@example.com",
"created_at":"2018-09-02 22:12:31",
"updated_at":"2018-09-25 00:24:59",
}
},
{
"id":4,
"user_id":1,
"content":"Broke rule 1",
"reportable_id":101,
"reportable_type":"App\\Post",
"reportable_created_at":"2018-11-29 20:27:56",
"created_at":"2018-11-29 20:27:47",
"updated_at":null,
"creator":{
"id":1,
"username":"Frank",
"slug":"Frank",
"name":"Frank Lettuce",
"email":"frank@example.com",
"created_at":"2018-09-02 22:12:31",
"updated_at":"2018-09-25 00:24:59",
}
}
],
"comments":[
{
"id":1,
"content":"Godfather ipsum dolor sit amet. Only don't tell me you're innocent. Because it insults my intelligence and makes me very angry. If anything in this life is certain, if history has taught us anything, it is that you can kill anyone. Don't ever give an order like that again. Not while I'm alive. I want your answer and the money by noon tomorrow. And one more thing. Don't you contact me again, ever. From now on, you deal with Turnbull. I have a sentimental weakness for my children and I spoil them, as you can see. They talk when they should listen.",
"post_id":101,
"user_id":1,
"created_at":"2018-09-02 22:15:57",
"updated_at":"2018-09-02 22:15:57",
"post":{
"id":101,
"user_id":1,
"title":"GREATEST SKATEBOARDING TRICKS EVER 2018! #1 BEST SKATE & SKATEBOARD & SKATING TRICKS COMPILATION",
"slug":"greatest-skateboarding-tricks-ever-2018-1-best-skate-skateboard-skating-tricks-compilation",
"content":"GREATEST SKATEBOARDING TRICKS EVER 2018! #1 BEST SKATE & SKATEBOARD & SKATING TRICKS COMPILATION. Skateboarding is one of the most popular types of extreme s...",
"created_at":"2018-09-02 22:14:27",
"updated_at":"2018-09-22 06:38:26",
}
}
]
}
],
"first_page_url":"http://127.0.0.1:8000/post-reports?page=1",
"from":1,
"last_page":1,
"last_page_url":"http://127.0.0.1:8000/post-reports?page=1",
"next_page_url":null,
"path":"http://127.0.0.1:8000/post-reports",
"per_page":15,
"prev_page_url":null,
"to":1,
"total":1
}
刀片模板
<h1>Reports For Posts</h1>
<hr>
@foreach($posts as $post)
<ul>
<li>
<h4><a href=" forums/{{$post->slug}} ">{{ $post->title }}</a></h4>
<span class="social-share-title pull-left text-capitalize">
By <a href="#">{{ $post->creator->username }}</a>
{{$post->created_at->format('F d, Y') }}
</span>
</br></br>
<ul>
{{$post->reports}}
</ul>
</li>
</ul>
<hr>
@endforeach
问题
由于某种原因,当我尝试使用刀片获取帖子报告时,我在屏幕上看到的都是“ 0”
当我尝试遍历报告时,出现错误:
<ul>
@foreach($post->reports as $report)
<li>
{{ $report->content }} <i> By {{ $report->user }}</i>
<p>{{$report->created_at}}</p>
</li>
@endforeach
</ul>
为foreach()提供的参数无效(查看:/Applications/MAMP/htdocs/community/resources/views/channels/reports/post-reports.blade.php)