lundi 14 octobre 2019

Cache config table and access it globally Laravel

I've a Configuration model with its table in database named configurations.

It has 3 columns: id, key, and value

I'd like to cache all the key/value pairs from this table for better performance and less database transactions.

What I've done so far: I've created a Provider named ConfigurationsServiceProvider and following is the code for register() method:

public function register()
{
    $this->app->singleton('configurations', function ($app) {
        return $app['cache']->remember('configurations', 60, function () {
            return Configuration::pluck('key', 'value')->toArray();
        });
    });
}

I've registered the provider in config/app.php as well.

Now I want to achieve the following:

  • Access these configuration key/value pairs globally by calling a method for example configurations('key')
  • Configurations data should be cached forever, for all users. It should only be re-cached when user inserts/updates any key/value record in database table
  • Not important but it would be really great if I could somehow merge and access the configurations data with Laravel framework's global config method.

Also, kindly suggest if there's any better way, other than .env, to store dynamic config data for application from user interface.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/33zifBJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire