How to bring only the latest records with Laravel 5.2

Asked

Viewed 1,294 times

2

I need to return the last 10 records to a view..

return view('home.home', [
    'data' => $this->sale->get(), //aqui eu quero pegar os 10 últimos
    'nav' => 'dashboard'
]);

There’s some Eloquent method that does that?

1 answer

4


Has the method is take, but it is important that the ordination (orderBy), to have no surprises in the result.

$this->sale->orderBy('id','desc')->take(10)->get()

return view('home.home', [
    'data' => $this->sale->orderBy('id','desc')->take(10)->get(), 
    'nav' => 'dashboard'
]);
  • 1

    It worked, thank you, I will accept the answer as soon as the system allows

  • 1

    The only thing is that to bring the last I put in order ^^

  • 1

    Changed to your comment @Felipepaetzold!!!

Browser other questions tagged

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