doudengjin8251 2017-03-15 22:59
浏览 96

Laravel多数据库,一个DB存储用户

I have an standard Laravel application I built for a company. I am now making this into a SAAS model but curious the best setup. I know for sure I would want each company to have its own DB for security, easier ability to get specific client data and since these are all competing companies it might just be a nicer selling point.

My issue is how I can set up a but of DBs and sub domains to point to their specific DB and application set up but I dont really want 100 sub domains and for an iOS application I need one single user auth portal.

Is there a specific way to have a db that holds all the laravel users and on login it gives them to their specific DB? Basically I just need one portal (auth location).

I know this is generic but not sure where to even post this or what to search for.

Setup:

user db (standard laravel setup)

User | Password Reset | Migrations | Roles

company a db

accounts | routes

company b db

accounts | routes
  • 写回答

2条回答 默认 最新

  • douxi3977 2017-03-15 23:19
    关注

    You have a default database for your application. That's where user authenticate. Each company will then have their db details stored on your main database. As soon as someone authenticates, you take the db details and make it as default.

    public function setDefaultConnection($company)
    {
        //update the config
         config(['database.connections.mysql' => [
            'host'     => $company->host,
            'username' => $company->username,
            'password' => $company->password
        ]]);
    
        //Check the credentials by calling PDO 
        try {
            DB::connection()->getPdo();
        } catch (\Exception $e) {
            return redirect()->back()->withErrors(["connection" => "Could not connect to the database.  Please check your input."]);
        }
    }
    

    Not sure it's the best approach, but it lets you at least change connection details on the fly.

    On the other hand, you could avoid updating existing and actually add a new one on the fly

    config(['database.connections.'.$company->id => [
        'driver'   => 'mysql',
        'host'     => $company->host,
        'username' => $company->username,
        'password' => $company->password
    ]]);
    
    //Using it
    $clients = DB::connection($company->id)->select(...);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀