我想顯示與特定類別關聯的帖子。類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