Slide 7
Slide 7 text
長くなりますよね・・・
public function show(Post $post)
{
$categories = Category::all();
foreach ($categories as $key => $category) {
$category->count = Category::where('id', $category->id)->count();
$categories->put($key, $category);
}
$searchFormAttributes = [
'type' => PostType::all()->pluck('value', 'id'),
'max' => 120,
'categories' => $categories->push(['name' => 'なし', 'id' => ''])->pluck('name', 'id'),
];
if (Auth::check()) {
$user = Auth::user();
} else {
$user = new User;
}
$relatedPosts = $post->relatedPosts;
if ($relatedPosts->count() < 5) {
$addPosts = Post::orderBy('id', 'desc')->take(5 - $relatedPosts->count())->get();
$relatedPosts = $relatedPosts->merge($addPosts);
}
return view(
'posts.show’,
compact('posts', 'categories', 'searchFormAttributes', 'user', 'relatedPost’)
);
}