Imagine you have the following columns in your "Article" model. If you have 15000 articles and you are getting it by using the "all" method, then highly likely the system will take time to load your data.
- id
- title
- body
- image
- links
- created_at
- updated_at
So instead of querying using "all" method like below.
Before
$articles = Article::all(); foreach($articles as $article) { dd($article); }
You can instead specify the only column that's necessary.
After
$articles = Article::select('id, 'title', 'image')->get(); // or $articles = Article::get(['id', 'title', 'image']);
By doing so, the query speed will be much faster and your computer/server can save ton of memory.
Do note that the code above is only for an example, if you have those many records then it's better to "paginate" or load it in "chunks".
Leave a reply