Skip to content
Advertisement

Trying to get property ‘cover_image’ of non-object (View: C:UsersBWBDocumentsLaravelasperablogsresourcesviewsblogsblogs.blade.php)

So I’m making a blog website and I’m implementing tags. I keep getting the error in the title and not sure what I’m supposed to do. I’ve looked through similar questions here but they look different from how i’ve done it. Im using pivot tables for the the tags. When I did it with the posts only it worked well and displayed everything here is the index method from my posts controller.

JavaScript

here is the index method from my tag controller.

JavaScript

Here is how I’m outputting it in the view

JavaScript

This is my tags model

JavaScript

Advertisement

Answer

Error

Your error comes from the fact that the variable $post within your foreach loop has returned as a non-object.


Probable Cause

That $posts is not being returned as a collection but rather a query builder instance

JavaScript

If you have built an eloquent relationship between a Tag model and a Post model, when you access this as a method (i.e. $tag->posts()) you will get an eloquent query builder instance. If you access this as a property (i.e. $tag->posts), it will return an eloquent collection.


Suggestion

Try passing the posts to the view as a collection

JavaScript

And try using a @forelse loop to catch the instances where you have no posts

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement