Я пытаюсь сохранить данные формы в приложении laravel в методе store.
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');
}
}
I включили весь необходимый импорт для мультитенанта. Но когда я пытаюсь запустить свой метод store и создать новую запись, я получаю
ErrorException
Trying to get property 'tenant_id' of non-object
ошибку, и дБ или запись в таблице приложения не создаются.