Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JDerby's avatar

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

0 likes
6 replies
jimmy0699's avatar

you may call View::share in mine Controller constructor or in web middleware, you have db started by then

TylerODonnell's avatar

If you use a view composer, the callback won't be triggered until the view is rendered.

View::composer('*', function ($view) use ($categoryRepository) {
    $view->with('publicationCategories', $categoryRepository->getAll());
});
1 like
JDerby's avatar

@TylerODonnell Thank you, if there is no way to do this with View::share then I will conceed defeat.

I had seen this option, but don't like the idea of loading this data to every view with the asterix wildcard, won't this load the same data multiple times? Isn't that the point of View:share? (maybe not)

I attempted to do this:

View::share('publicationCategories', function() use ($categoryRepository){

     return $categoryRepository->getAll();

});

But this is not documented and a guess. Totally wrong no doubt.

I understand why accessing the database is an issue in the SP. But I just feel like View::share should have a callback method the same as the composer for this very scenario.

I wonder if the below would work? (Cant test right now).

View::share('publicationCategories', function($view) use ($categoryRepository){

     $view->with('publicationCategories', $categoryRepository->getAll());

});
TylerODonnell's avatar
Level 28

@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

2 likes

Please or to participate in this conversation.