A
A
alex-php2016-04-01 18:45:56
Laravel
alex-php, 2016-04-01 18:45:56

Am I correct in my reasoning about working with the database in Laravel?

For example, I have a Model Article inside that stores information about Articles
, for example, if I write inside ArticleController:
$article = Article::where('id', 1)->first();
return view('common.article')->with('article', $article);
then in the $article view, referring to {{$article->name}}, I will once again pull the database with the request:
SELECT `articles`.`name` FROM `articles` WHERE `articles`.`id` = 1;
which is not entirely good, because if I cycle through all the articles, I will form a new request every time. In theory, I need to do this
$article = Article::where('id', 1)->first()-toArray(); and then to transfer in I twist.
Am I correct in my reasoning about working with the database in Laravel?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kuznetsov, 2016-04-01
@dima9595

No, outputting to the template via "view('common.article')->with('article', $article);" - you request data only once.

S
Stanislav Pochepko, 2016-04-01
@DJZT

Install the laraveldebugbar extension. Can't give Link. I'm calling from phone. But in Google there is immediately. With it, you can see how many and what queries are made in the database. Helpful for understanding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question