Answer the question
In order to leave comments, you need to log in
How to shorten requests in Laravel?
Started learning laravel, made a simple website with authorization. Now I installed Laravel Debugbar to see SQL queries. And I saw such a thing.
There are a lot of identical requests for obtaining data about users from the database. I understand this because I use Auth::user() many times in Blade templates. Tell me how to reduce the number of requests, as I understand it, you will have to make a variable in the controller $user = Auth::user() and then pass it to the view. But then, isn't the meaning of the Blade templates lost, and all these Auth::check(), maybe I don't understand something?
Answer the question
In order to leave comments, you need to log in
Use statics, in the method where this request is made, do the following
function func(){
static $user;
if (!$user) {
$user = ''; // тут запрос на получение данных
}
return $user;
}
Now you can call the function as much as you likefunc()->id;
func()->id;
func()->id;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question