如何在laravel 5.3

时间:2016-11-24 23:55:59

标签: laravel-5.3

我正在尝试从数据库中获取值并传递给自定义settings.php文件,在laravel 4中,我可以从模型中设置值:$ settings = Setting :: find(1);但是在laravel 5.3中,config文件夹就是app app文件夹。 获取值并传递给settings.php文件的最佳方法是什么? 也许添加服务提供商?我怎样才能做到这一点?

class SettingServiceProvider ...

public function register()
{
    $settings = Setting::find(1);
    Config::set('settings.company', $settings->company);
    return $settings;
}

也许写在.env?但是,如果用户想要更新这些值,我该如何从用户界面更新.env?

请帮忙! 提前谢谢。

1 个答案:

答案 0 :(得分:1)

以下是我在Config中用来连接数据库

的示例

我创建了Config中间件并在路由中使用它。在我的情况下,我有多租户申请,我相信它也会帮助你。 示例

创建名为namespace App\Http\Middleware; use App\Sites; use Closure; class config { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $site_url = $_SERVER['SERVER_NAME']; $site = Sites::where('site_url',$site_url)->first(); $config = array(); if(!is_null($site)){ $config['id'] = $site["id"]; $config['site_url'] = $site["site_url"]; $config['theme'] = $site["theme"]; $config['host'] = $site["host"]; $config['db_name'] = $site["database"]; $config['user'] = $site["user"]; $config['password'] = $site["password"]; $config['prefix'] = $site["prefix"]; }else{ //default values $config['id'] = "0"; $config['site_url'] = "demo.local"; $config['theme'] = "theme"; $config['host'] = "localhost"; $config['db_name'] = "dbname"; $config['user'] = "username"; $config['password'] = "password"; $config['prefix'] = "tbl"; } //dd($config); // \Session::put('config', $config); \Config::set('database.connections.mysql.host', $config['host'] ); \Config::set('database.connections.mysql.database', $config['db_name'] ); \Config::set('database.connections.mysql.username', $config['user']); \Config::set('database.connections.mysql.password', $config['password']); \Config::set('database.connections.mysql.prefix', $config['prefix']); \Config::set('database.connections.mysql.theme', $config['theme']); \DB::reconnect(); return $next($request); } }

的中间件
Route::group(['middleware' => 'config'],function (){
       Route::get('/', ['uses' => 'HomeController@page']);
});

现在在您要应用该配置的路线中,请执行以下操作

id     device_id           punch          stat_log
 1       26           2016-11-23 11:30:05    in
 2       26           2016-11-23 14:30:05    out
 3       26           2016-11-23 15:00:05    in
 4       26           2016-11-23 20:32:05    out