В laravel я получил отношение многие ко многим между узлами и пользователями:
Вершина
public function up()
{
Schema::create('nodes', function (Blueprint $table) {
$table->bigIncrements('id')->index();
$table->string('syncState')->nullable();
$table->integer('jsonRpcPort')->nullable();
$table->string('addr')->unique()->index();
$table->BigInteger('height')->nullable();
$table->string('nodeId')->nullable();
$table->string('publicKey')->nullable()->index();
$table->integer('websocketPort')->nullable();
$table->integer('relayMessageCount')->nullable();
$table->integer('sversion')->nullable();
$table->string('version')->nullable();
$table->timestamps();
});
}
В Node-Model:
public function users()
{
return $this->belongsToMany('App\User')->withPivot('hostname', 'label', 'notified_offline', 'notified_outdated', 'notified_stuck');
}
Пользователи
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->boolean('verified')->default(false);
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
В пользовательской модели:
public function nodes()
{
return $this->belongsToMany('App\Node')->withPivot('hostname', 'label', 'notified_offline', 'notified_outdated', 'notified_stuck');
}
Node_user
{
Schema::create('node_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('node_id');
$table->string('hostname')->nullable();
$table->string('label')->nullable();
$table->timestamp('notified_offline')->nullable();
$table->timestamp('notified_outdated')->nullable();
$table->timestamp('notified_stuck')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->foreign('node_id')
->references('id')->on('nodes')
->onDelete('cascade');
});
}
Теперь, если я позвоню $user->nodes()->detach();
, чтобы отсоединить все узлы от пользователя, я также хочу, чтобы - если ни один другой пользователь не был присоединен - эти узлы также должны быть удалены из базы данных.
Как мне это заархивировать? Я использую postgresql кстати.