Home » Php » Laravel 5: How can I load database values into the config/services.php file?

Laravel 5: How can I load database values into the config/services.php file?

Posted by: admin December 7, 2017 Leave a comment

Questions:

I have a multi-tenant app I’m working on and while adding the socialite package, I tried to load the custom facebook client_id and client_secret for the specific website from the database. I can’t really use the env variables because each site will have it’s own custom facebook keys.

It seems you can’t really call a model’s method on the config/services.php file because it might not have been loaded yet. I’ve tried going through the request lifecycle docs to resolve this to no avail.

I’ve also tried to create a service provider to get the value from my Business model’s method and set it as a constant but still, by the time it’s available in the app, the config/services.php file has been loaded.

Here’s where I want the database value available:

config/services.php

'facebook' => [
  'client_id' => \App\Business::getAppKeys()->fb_client_id,
  'client_secret' => 'your‐fb‐app‐secret',
  'redirect' => 'http://your‐callback‐url',
],

Error:

Fatal error: Call to a member function connection() on null

Answers:

You really should not want to do this. Initialising DB connections from a model requires all config to be loaded, and you intend to use these connections to define your config. You’ve found yourself having a circular dependency problem.

I’d like to suggest having a look at the socialite package you’re trying to use. If no facilities exist in the service to set/override credentials at runtime, see if you’re able to extend the service with your own implementation that does allow for that. I think that will be the only way to accomplish what you’re trying to do.

Besides all that, config files are supposed to be composed of only scalar values and arrays, so that they can be cached.

Questions:
Answers:

Here’s a really quick example of what I’m talking about.

Using the following, I am able to query the database, grab some details and write them to a config file, before the rest of my application is initialised.

See how I’m able to access the value in a route?

// app/Providers/ConfigServiceProvider.php

namespace App\Providers;

use App\Models\Country;
use Illuminate\Support\ServiceProvider;

class ConfigServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $countries = Country::pluck('name', 'iso_code');
        config()->set(['app.countries' => $countries->toArray()]);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}


// config/app.php

// ...

'providers' => [
    // ...
    App\Providers\ConfigServiceProvider::class,
    // ...
],

// ...


// routes/web.php

Route::get('config', function () {
    dd(config('app.countries'));
});

Leave a Reply

Your email address will not be published. Required fields are marked *