我的助手有一个功能
function somethingOrOther($id)
{
$posts = Posts::where('author_id', $id);
$posts_count = [];
$posts_count['total'] = $posts->count();
$posts_count['published'] = $posts->where('status', 'published')->count();
$posts_count['draft'] = $posts->where('status', 'draft')->count();
return $posts_count;
}
这个功能完美无缺。但是现在我正在尝试在控制器中使用它来将它返回到视图
public function profile($id)
{
$posts_count[]= somethingOrOther($id);
return view ('display.profile')->withPosts_count($posts_count['total'])->withPosts_published_count($posts_count['published'])->withPosts_draft_count($posts_count['draft']);
}
但我收到'未定义索引:总计'的错误。我需要做什么来单独返回$ posts_count的数组值?
答案 0 :(得分:0)
尝试
$post_count = $this->somethingOrOther($id);
if ( !$post_count ) {
return "no posts for this author";
}
...
return view('display.profile', compact('post_count'));
在视图中
{{ $post_count['total'] }}
答案 1 :(得分:0)
我相信你可以做到
$posts_count= somethingOrOther($id);
然后像
一样返回return view ('display.profile',["posts_count" => $posts_count]);
并且在刀片中得到它
{{$posts_count["total"]}}