Help to figure out where to shove the method ->paginate(10) .
This is a query to the database:

 public function getAllNews() { return $this->orderBy('seriatim', 'desc')->where(['active' => 1])->get(); } 

This is a call to the method of receiving all news in the controller:

 $Allnews = $news->getAllNews()->toArray(); $i = 0; foreach ($Allnews as $value) { $value['created_at'] = Carbon::createFromFormat('Ymd H:i:s', $value['created_at'])->format('dmY / H:i:s'); $Allnews[$i]['date'] = $value['created_at']; $i++; } 

After calling the getAllNews() method, I use, foreach to add the key with the necessary date and time format to the same array.
Well and then, I create a variable for the view:

 $data = [ 'Allnews' => $Allnews, 'MainMenu' => $menu->getMainOptions()->toArray(), 'SubMenu' => $subm->getSubOptions()->toArray(), ]; 

How to paginate news here?

  • Welcome to Stack Overflow in English! If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - VenZell

1 answer 1

You have the wrong approach in principle. You first get all the news (and there may be a lot of them) translate it into an array and iterate over all the elements of the array for a simple task.

Add a date method to the model. The content of the method is approximately as follows:

 public function date() { return Carbon::createFromFormat('Ymd H:i:s', $this->created_at)->format('dmY / H:i:s'); } 

Now you will not need to translate into an array and sort through the elements. ->paginate(10) method ->paginate(10) and at the right time call your method ->date()

  • Thank! With pagination figured out the grief hit! - overtheman
  • Now created in the Model, the date method, but the call in the controller does not plow. $ Allnews = $ news-> getAllNews () -> date (); swears at such a line in that "The date () method does not exist" - = ((( - overtheman
  • @overtheman $ news-> getAllNews () - gets all the news lines (roughly an array). When you display them on the page (for example, through foreach) call the date () method for each line. - Alexander Andreev
  • I would also recommend using paginate () instead of get () so you will not get all the records but only those that you need. - Alexander Andreev
  • I do not recommend giving thanks. But alas, I can't raise my reputation - = ((I figured it all out, everything works! Thanks for the help, I am very grateful! - overtheman