N
N
NikSIk312019-10-13 20:44:03
Laravel
NikSIk31, 2019-10-13 20:44:03

LaravelViewServiceProvider?

there is a ViewServiceProvider in which there is View::composer('path to the template', a function that receives data from the database and returns it to via $view->with()), it so happened that this view is used twice on one page -> there are two identical requests to the database. Is there any way to solve this? For example, make a request not in the view itself, but BEFORE it and after passing the finished data in order to then pass it to the view through the same $view-> with ()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sokharev, 2019-10-17
@greabock

Crutch on static

View::composer('dashboard', function ($view) {
     static $result;
     if(!$result) {
        $result = getSomeData();
    }
    $view->with($result);
});

In addition, instead of a function, you can assign a class, which is defined in the container as a singleton, and instead of a static one (as in the example above), use the internal fields of the class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question