S
S
SuperDoker2017-09-01 21:59:05
Laravel
SuperDoker, 2017-09-01 21:59:05

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.
eEjy4TXkTYCQcKChmMIKdg.png
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

3 answer(s)
M
Maxim Fedorov, 2017-09-02
@Maksclub

Looks like lazy loading

A
Alexander Shapoval, 2017-09-01
@sanek_os9

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 like
func()->id;
func()->id;
func()->id;

Request to be executed only once

D
dskozin, 2017-09-07
@dskozin

You would have published the template code so that they could help you, otherwise you will blindly get not help, but anti-patterns.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question