2
votes

I have a list containing article posts from all categories. I want to display the category tag in each post on the list and I tried this :

<article class="post bg z-depth-1">
                    <?php foreach ($article_list as $article): ?>
                    <div class="post-img">
                        <a href="post.html">
                            <img class="retina" src="{{ asset('image/' . $article->image) }}" width="820" height="500" alt="Post Image">
                        </a>
                    </div>
                    <div class="post-content">
                        <div class="post-header center-align">
                            <div class="tags">
                                <a href="#">{{ $article->category->title }}</a></div>
                            <h2 class="post-title"><a href="post.html">{{ $article->title }}</a></h2>
                            <div class="date">Posted on {{ Carbon\Carbon::parse($article->created_at)->format('F j, Y') }}</div>
                        </div>
                        <div class="post-entry">
                            <p>sss</p>
                        </div>
                        <div class="post-footer">
                            <div class="post-footer-item">
                                <a href="post.html" class="link"><span class="text">Read More</span> <i class="fa fa-arrow-circle-right"></i></a>
                            </div>
                        </div>
                    </div><!-- .post-content -->
                    <?php endforeach ?>
                </article><!-- .post -->

and it gave me error Undefined property: stdClass::$category and I don't know why, I already define the relationship in each model Article and Category. Thanks for the help!

In Article model :

public function category()
    {
        return $this->belongsTo('App\Category', 'category_id');
    }

In Category model :

public function article() {
        return $this->hasMany('App\Article', 'category_id');
    }

Controller :

public function index() {
        $article_list = Article::all();
        return view('article/index', compact('article_list', $article_list));
    }
3
Please show your relationships. - GabMic
@GrowingDev I've updated my question - Ariando Miller
in your models, please remove the category_id field and try it. - GabMic
@GrowingDev didn't work :( - Ariando Miller
Please show your controller. - GabMic

3 Answers

2
votes

What you need to do is eager load those relationships in your controller.

When you pull the records do it like this:

$article_list = Article::with('category')->get();

https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

0
votes

Change this: $article->category->title

To this: $article->category()->title

When you define the relationship in your model you are also creating a function not a property called category.

0
votes
public function category()
    {
        return $this->belongsTo('App\Category');
    }

In Category model :

public function article() {
        return $this->hasMany('App\Article');
    }

Controller :

public function index() {
        $article_list = Article::all();
        return view('article/index', compact('article_list'));
    }

This should work for you now like this.