Answer the question
In order to leave comments, you need to log in
How to correctly pass data to the display?
Hello. Let's say you want to display a list of all blog articles. I create a controller action:
$posts = Post::all()->toArray();
return view('posts', ['posts' => $posts]);
User::chunk(200, function($users) {
foreach ($users as $user) {
//
}
});
use App\Post;
and using the methods of the model is hardly the right solution.
Answer the question
In order to leave comments, you need to log in
Personally, I don't see anything wrong with using objects in the View. What do you gain by casting an object to an array? Are you unbinding the template from the model? Pretty dubious benefits, but the problems are obvious.
There is a very detailed discussion here .
To answer your question, it should be like this:
$posts = Post::all();
return view('posts', ['posts' => $posts]);
$posts->chunk(200, function($chunk) {
foreach ($chunk as $post) {
//
}
});
In fact, the problem is contrived.
What is your goal by displaying > 200 objects on a page?
Why not just use the paginate method and paginate the results? Then there will be no problem with over9000 objects on the page.
Displayed 500 users per page with 8 fields, 3 of which are relationships. (Role, Group, Number of posts), but at the same time I used the method in the controller User->with('role')
and did not notice anything. Limit in PHP is 384MB
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question