In my laravel-application I have a blogs- and an author-table. On the index page, where you see all published blog posts, I want the authors name to appear. So I tried to do this:
public function index()
{
$blogs = Blog::where("publishes_on", "<=", Carbon::now())
->orderBy('publishes_on', 'desc')
->published()
->get();
foreach ($blogs as $blog) {
$author = Author::where('id', $blog->author_id)->get();
}
return view('app.blog.index', compact('blogs', 'author'));
}
For some reason I do not know, this gives me the last added author to my application and on each post, the name of that author is displayed on all posts.
What am I doing wrong here?
Blog belongs to Authorrelationship and get the author`s details via that relationship