Geting ErrorException试图获取Laravel中非对象的属性

问题描述 投票:0回答:1

我正在尝试通过store方法将表单数据存储在laravel应用程序中。

public function store(Request $request)
    {
        $this->validate($request, [
            'subDomainName' => 'required',
            'subDomainSuffix' => 'required',
            'lang' => 'required',
            'themeid' => 'required',
            'paymentoption' => 'required',
            'packageType' =>'required',
            'domain' => 'unique:apps',

        ]);

        $user = Auth::user();
        $fullDomain = $request->domain;
        $dbName = $this->dumpNewDB($fullDomain);
        $appId = $this->getNextId();
        // create record in app table
        Website::create([
            'domain' => $fullDomain,
            'masterUserId' => $request->user,
            'dbName' => $dbName,
            'host' => env('DB_HOST', '127.0.0.1'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', 'root'),
            'theme' => $request->themeid,
            'lang' => $request->lang,
            'status' => 1,
            'package_type' => $request->packageType,
            'payment_option' => $request->paymentoption,
            'isAppCreated' => 1,
            'isDefault' => 0,
        ]);
    }

而且我也有一个转储数据库的功能

public function dumpNewDB($domain)
    {
        Tenant::new()->withDomains($domain)->save();
        $tenant = DB::table('domains')->where('domain', $domain)->first();
        \Artisan::call('tenants:migrate', [
            '--tenants' => [$tenant->tenant_id]
        ]);
        \Artisan::call('tenants:seed', [
            '--tenants' => [$tenant->tenant_id]
        ]);

        return $tenant->tenant_id;
    }

[每当我运行以下功能时,都会出现以下错误,

ErrorException
Trying to get property 'tenant_id' of non-object

我的应用程序表的数据库迁移如下,

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAppsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('apps', function (Blueprint $table) {
            $table->bigIncrements('appId');
            $table->string('domain')->unique();
            $table->bigInteger('masterUserId')->unsigned();
            $table->string('dbName');
            $table->string('host');
            $table->string('username');
            $table->string('password');
            $table->string('theme');
            $table->string('lang');
            $table->date('renewDate')->nullable();
            $table->date('renewedDate')->nullable();
            $table->tinyInteger('status');
            $table->bigInteger('package_type')->unsigned();
            $table->string('payment_option');
            $table->tinyInteger('isAppCreated');
            $table->string('stripeCustomerId')->nullable();
            $table->tinyInteger('subscrStatus')->nullable();
            $table->dateTime('reSubTime')->nullable();
            $table->dateTime('upgradeTime')->nullable();
            $table->tinyInteger('isDefault')->nullable();

            $table->foreign('masterUserId')
                ->references('id')
                ->on('users');

            $table->foreign('package_type')
                ->references('id')
                ->on('packages');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('apps');
    }
}

我的租户的数据库迁移,

<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTenantsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('tenants', function (Blueprint $table) {
            $table->string('id', 36)->primary(); // 36 characters is the default uuid length

            // (optional) your custom, indexed columns may go here

            $table->json('data');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('tenants');
    }
}

和域,

<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDomainsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('domains', function (Blueprint $table) {
            $table->string('domain', 255)->primary();
            $table->string('tenant_id', 36);

            $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('domains');
    }
}

我已包括多租户所需的所有进口商品。但是,每当我尝试运行store方法并创建新记录时,我都会得到>

ErrorException
Trying to get property 'tenant_id' of non-object

错误和租户数据库或应用程序表中的记录未创建。

我正在尝试通过store方法将表单数据存储在laravel应用程序中。公共功能存储区(请求$ request){$ this-> validate($ request,['subDomainName'=> ...

php laravel multi-tenant laravel-6 laravel-7
1个回答
0
投票
public function dumpNewDB($domain)
    {
        Tenant::new()->withDomains($domain)->save();
        $tenant = DB::table('domains')->where('domain', $domain)->first();
        dd($tenant);
     }
© www.soinside.com 2019 - 2024. All rights reserved.