drcj64241 2017-07-29 05:56
浏览 260
已采纳

Laravel 5:如何将数据库值加载到config / services.php文件中?

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

  • 写回答

2条回答 默认 最新

  • doudoulb1234 2017-08-25 21:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!