从 Stancl/ Laravel Tenancy 的租户域内的中央域获取意外数据

Getting unexpected data from central domain inside the tenants domain in Stancl/ Laravel Tenancy

我正在使用 Stancl/TenancyforLaravel 租赁系统。

我已经为租户和中心域用户创建了一个名为 PaymentSettings 的模型和迁移。

对于中央用户 = app\Models\System\Admin\PaymentSettings.php

对于租户用户=app\Models\Tenant\Admin\PaymentSettings.php

我添加了 crud 操作,运行 绝对没问题。 我在启动时在服务提供商中设置条带 API 时面临挑战。 当我从数据库中获取设置数据时

文件:app\Providers\TenancyServiceProvider.php

namespace App\Providers;
use App\Models\System\Admin\SmtpSettings as AdminSmtpSettings;
use App\Models\Tenant\Admin\AdvanceSettings;
use App\Models\Tenant\Admin\GeneralSettings;
use App\Models\Tenant\Admin\PaymentSettings;
use App\Models\Tenant\Admin\SmtpSettings;
 ...

public function boot()
    {
        $this->bootEvents();
        $this->mapRoutes();        
        $this->makeTenancyMiddlewareHighestPriority();
        $this->viewResource();
    }
 ....
   protected function viewResource(){
        View::composer('*', function ($view) {
            $this->setConfigData();
            if(Schema::hasTable('general_settings')){
                $generalSettings = GeneralSettings::findOrFail(1);
                $advanceSettings = AdvanceSettings::findOrFail(1);
                $view->with([
                        'tenant_general' => $generalSettings,
                        'tenant_advance' => $advanceSettings,
                ]);
            }
        });
    }
 protected function setConfigData()
    {

        // When tenancy exists fetch the data from the tenants data;
        $smtpTableExists = Schema::hasTable('smtp_settings');
        $smtpSettings = null;
        if(tenancy()->tenant){
            if($smtpTableExists){  $smtpSettings = SmtpSettings::findOrFail(1); }
           
            // Stripe Payment settings
            $paymentTableExists = Schema::hasTable('payment_settings');
            if($paymentTableExists){
                $paymentSettings = PaymentSettings::findOrFail(1);
                // return dd($paymentSettings->key);
                if($paymentSettings && $paymentSettings->method === 'Stripe'){
                    Config::set('services.stripe.key', $paymentSettings->key);
                    Config::set('services.stripe.secret', $paymentSettings->secret);
                    Config::set('services.stripe.webhook_secret', $paymentSettings->webhook_secret);
                    Stripe::setApiKey($paymentSettings->key);
                }
            }
        } // ends tenants config
        else { // fetch data from the central domain
            if($smtpTableExists){  $smtpSettings = AdminSmtpSettings::findOrFail(1); }
        }

        // set configs
        if($smtpSettings){
            Config::set('mail.mailers.smtp.host', $smtpSettings->host);
            Config::set('mail.mailers.smtp.port', $smtpSettings->port);
            Config::set('mail.mailers.smtp.encryption', $smtpSettings->encryption);
            Config::set('mail.mailers.smtp.username', $smtpSettings->username);
            Config::set('mail.mailers.smtp.password', $smtpSettings->password);
            Config::set('mail.from.address', $smtpSettings->from_address);
            Config::set('mail.from.name', $smtpSettings->from_name);
        }

    }
....

因为我已经设置了 API 键,我应该把它放在控制器里,但不是这样, 当我调用 Stripe::getApiKey()

时,我在其他控制器中得到值 null

我认为启动文件可能 运行 不正确,我尝试在租户域内的控制器构造函数中手动编写此文件作为

use App\Models\Tenant\Admin\PaymentSettings;

public function __construct()
    {
        $this->middleware('auth');
        $paymentSettings = PaymentSettings::findOrFail(1);
        Stripe::setApiKey($paymentSettings->key);
       return dd(Stripe::getApiKey()); // showing the incorrect value from the central domain
   } 

但是问题并没有到此为止, 我得到的值是来自中央域的值,而不是控制器内的租户域。 但是当我在租户控制器的任何其他方法中编写类似的东西时,它给出了正确的值。

use App\Models\Tenant\Admin\PaymentSettings;

public function index()
    {
         // return dd(Stripe::getApiKey()); // showing the incorrect correct value from central domain

        $paymentSettings = PaymentSettings::findOrFail(1);
        Stripe::setApiKey($paymentSettings->key);

          return dd(Stripe::getApiKey()); // showing the correct value

        return view(someviewfilename);
   } 
预期行为

我想在租户域中获取租户的数据库数据,而不是中心域数据库数据。

您的设置

我从创作者那里得到了答案。 https://github.com/stancl/tenancy/issues/558

Think about how the Laravel request lifecycle works.

Here you're defining some middleware specifics: enter image
description here

And on the next line, you expect the middleware to be already executed? Of course it's not, Laravel first executes service providers and controller constructors, and only then does it know what middleware to use.