我想显示与特定类别关联的帖子。类stdClass的对象无法转换为字符串|数据透视表
我有一个预先定义的类别表,每个类都与一个唯一的ID相关联。
我有一个职位表,我有一个数据透视表,链接两个称为category_post。
数据透视表由category_id & post_id组成。
我想要查询数据透视表来恢复与特定category_id关联的所有post_id。
我的控制器需要所选类别的ID的参数:
public function getCategoryPost($id)
{
$selectedID = DB::table('category_post')->select(['post_id'])->where('category_id', '=', $id)->get();
$posts = Post::find($selectedID);
return View::make('posts.category')->with('posts', $posts);
}
现在我想显示在叶片上的结果,但该职位的只有标题:
@foreach($posts as $post)
class="post-title"> {{$post->title}}
@endforeach
这是我的,我得到以下错误“class stdClass的对象无法转换为字符串”
有什么建议吗?
'$ selectedID'是一个对象。 Post :: find()把它变成一个字符串。只是一个猜测。你应该'var_dump($ selectedID);'。 – 2014-09-03 09:40:14
** $ selectedId **是一个对象,你确定** Post :: find()**需要对象,我不这么认为。 – anwerj 2014-09-03 09:40:23