Reputation: 499
Ok, so I'm loading a lot of partials that present user data throughout my app. I'd like to avoid writing the same code over and over again for what is essentially the same piece of information. Like name, description and profile picture.
Should I be pulling directly from the database? Should I use base models and always pull from them? Is there a way to instance information that will be repeated without constantly calling the database?
Upvotes: 1
Views: 228
Reputation: 19285
That is a good use case for a View Composer :
//everytime one of these partials is build, call the closure...
View::composer(['partial_1', 'partial_2'], function( $view )
{
//get the data needed in the partial
$data = /* code */
//pass the data to the partial
$view->with([ 'data' => $data ]);
} );
When one of the listed partials is build, Laravel will call the Closure, get the data and pass it to the partial
So you have a unique point in which you fetch the data from the db and pass it to every partials needing it
Upvotes: 1
Reputation: 119
You can use cache data. Laravel provides supports popular caching backends like Memcached and Redis out of the box. You can use database or file based cache. Please read full features and uses of cache in laravel from https://laravel.com/docs/5.3/cache
Upvotes: 1