Laravel 5: How to load database values ​​into config / services.php file?

I have an application with several tenants I'm working on, and when adding the socialite package, I tried to load the custom facebook client_id and client_secret for a specific website from the database. I cannot use env variables because each site will have its own custom facebook keys.

It seems you really cannot call the model method in the config / services.php file, as it may not have been loaded yet. I tried to view the request lifecycle documents to resolve this to no avail.

I also tried to create a service provider to get the value from my business model method and set it as a constant, but still, by the time it is available in the application, the config / services.php file was loaded.

Here, where I want the database value to be:

configurations /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: function call of member function () at zero

+4
source share
2 answers

You really shouldn't do that. Initializing database connections from the model requires that the entire configuration be loaded, and you intend to use these connections to determine your configuration. You are faced with a circular addiction problem.

, . / , , , . , , .

, , .

+2

, .

, , , .

, ?

// 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'));
});
+1

Source: https://habr.com/ru/post/1682551/


All Articles