Я пытаюсь получить все сообщения, в которых есть отчеты, а затем нетерпеливо загружать отчеты.
Отношения
//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
}
Шаблон Blade
<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
Проблема
По какой-то причине, когда я пытаюсь использовать blade-сервер для получения почтовых отчетоввсе, что я получаю на экране, это "0"
![enter image description here](https://i.stack.imgur.com/5MgSd.png)
Когда я пытаюсь пройтись по отчетам, я получаю ошибку:
<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)