Doubt for view display

Asked

Viewed 21 times

1

Good evening, I’m trying to pull the contents of my database ordered by the created_at field, but in the view nothing appears, no error, but nothing from the bank appears on the other hand.

Code in the Homecontroller:

public function index()

{
    $postagens = Postagens::orderBy('created_at','desc');
    return view('index', compact('postagens'));
}

Code in view:

@foreach ($postagens as $pubs)
   @postagens()
      @slot('titulo')
         {{$pubs->nomePost}}
      @endslot

      @slot('descricao')
         {{$pubs->descricao}}
      @endslot

      @slot('nome')
         {{$pubs->usuario}}
      @endslot

      @slot('dia')
         {{$pubs->created_at}}
      @endslot

      @slot('id')
         {{$pubs->id}}
      @endslot        
   @endpostagens
@endforeach

1 answer

1


Try it this way

public function index()
{
    $postagens = Postagens::orderBy('created_at','desc')->get();
    return view('index', compact('postagens'));
}

Your code lacked the get method().

See the documentation.

  • Strange that is not appearing decreasingly yet, ta printing in order of insertion in the bank.

  • Try this: dd(Posts::toSql()) and see how this query is generated by Laravel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.