I want to display the post that is associated with a specific category.
I have a table for categories that are pre-defined, each associated with a unique id.
The I have a table for posts, I have a pivot table that links the two called category_post.
The pivot table consists of category_id & post_id.
I want to query the pivot table to bring back all the post_id associated with a particular category_id.
My Controller takes a parameter of the id of the selected category:
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);
}
Now I want to display the results in the blade but only the title of the post:
@foreach($posts as $post)
class="post-title"> {{$post->title}}
@endforeach
This is what I have, and I get the following error "Object of class stdClass could not be converted to string"
Any suggestions?
$selectedIDis an object.Post::find()turns it into a string. Just a guess. You shouldvar_dump($selectedID);. - Sverri M. Olsen