you may call View::share in mine Controller constructor or in web middleware, you have db started by then
View::share from service provider, error on migrate?
The problem is this:
I have a ViewShareServiceProvider.php
class ViewShareServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @param CategoryRepositoryContract $categoryRepository
* @return void
*/
public function boot(CategoryRepositoryContract $categoryRepository)
{
$publicationCategories = $categoryRepository->getAll();
View::share('publicationCategories', $publicationCategories);
}
/** etc.... **/
All is well.
Now, if you try and deploy this code it will break when you do
php artisan migrate --seed
This is because this service provider is being called during the migration process, which makes sense as it probably runs all service container bindings before doing anything else.
But then how do we handle service providers that access the database when there isn't a database yet..?
$categoryRepository->getAll();
Will call the database.
I am no doubt doing something backwards.
Thanks
@JDerby View share does not accept a callback. View Share gets primarily used for strings that don't hit the DB.
You are correct with the assumption the query would be running multiple times based on the number of views you load in. I would cache the query, so it only runs once.
View::composer('*', function ($view) use ($categoryRepository) {
$publicationCategories = \Cache::rememberForever('channels', function () use ($categoryRepository) {
return $categoryRepository->getAll();
});
$view->with('publicationCategories', $publicationCategories);
});
*Adjust the cache amount as necessary
Please or to participate in this conversation.