Home > Software engineering >  Get Posts on index page
Get Posts on index page

Time:10-10

I'm trying to get posts on Index page using laravel but getting errors.

public function index()
{
    $posts = Post::orderBy('created_at', 'desc');
    return view('pages.index')->with('posts', $posts);
}

The above snippet defines what I want to do, but, getting this error on index page:

count(): Parameter must be an array or an object that implements Countable

Same snippet works on other pages but it's not working on index page.

CodePudding user response:

Extract the results using get method like so:

$posts = Post::orderBy('created_at', 'desc')->get();
  • Related