Correct wat to get post data for each post in post loop?

Hi guys,

I’m trying to pass the current post category from the Controller to my Blade template. But when I call the controller function it’s running for each post and not just my current post in the post loop.

What am I doing wrong?

    <div class="postitem__image">
      <span class="postitem__label"> {{get_category()}} </span>
       public function getCategory()

        $categories = get_the_category(get_the_ID());

        return $categories;

  1. public methods in controllers are not available directly in Blades: What the Blade has access to is what the function returns. So in your case, that would be the $categories variable.
  2. That being the case, when you call get_category() in your Blade, you are not calling the method from your controller, you are calling the WordPress function get_category().
  3. You haven’t specified what Controller you’ve defined this method in, and you haven’t given us much context for where your Blade snippet appears either, which makes this very difficult to debug. I also don’t understand how “each post” is distinct from “my current post in the post loop”: as you move through the loop, each post becomes current for the purposes of execution.