delete with data that relationship

I have a form that have relationship (hasMany) to multiple participants. How to make when deleting the form from table resource, is also remove the participants data to?
8 Replies
mvenghaus
mvenghaus3mo ago
usually you define this using the foreign key in your database
mvenghaus
mvenghaus3mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
mvenghaus
mvenghaus3mo ago
->onDelete('cascade');
thyk123
thyk1233mo ago
Thank you so much for your respond! But sadly isn't working, I have 2 tables RootInformation and Participant. At the RootInformation model already had this
public function participant(): HasMany
{
return $this->hasMany(Participant::class,'idRootInfo','id');
}
public function participant(): HasMany
{
return $this->hasMany(Participant::class,'idRootInfo','id');
}
at the Participant model
public function RootInformation(): BelongsTo
{
return $this->belongsTo(RootInformation::class, 'id');
}
public function RootInformation(): BelongsTo
{
return $this->belongsTo(RootInformation::class, 'id');
}
works fine when using repeater relationship. but now when want to delete data from RootRelationship table, the participant not follow to deleted.
mvenghaus
mvenghaus3mo ago
how does your migration file look like?
thyk123
thyk1233mo ago
RootInformation migration:
<?php
Schema::create('root_information', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('gender');
$table->text('address');
$table->string('phoneNumber');
$table->string('email');
$table->string('instituteName');
$table->foreignId('idPriceCategory');
$table->string('proofPayment')->nullable();
$table->boolean('isVerified')->default(0)->nullable();
$table->timestamps();
});
}
<?php
Schema::create('root_information', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('gender');
$table->text('address');
$table->string('phoneNumber');
$table->string('email');
$table->string('instituteName');
$table->foreignId('idPriceCategory');
$table->string('proofPayment')->nullable();
$table->boolean('isVerified')->default(0)->nullable();
$table->timestamps();
});
}
participants migration
<?php
Schema::create('participants', function (Blueprint $table) {
$table->id();
$table->foreign('idRootInfo')->references('id')->on('RootInformation')->onDelete('cascade');
$table->string('nameParticipant');
$table->boolean('genderParticipant');
$table->date('dobParticipant');
$table->text('addressParticipant');
$table->string('phoneNumberParticipant');
$table->string('emailParticipant');
$table->string('instituteNameParticipant');
$table->string('pickupParticipant');
$table->foreignId('idClassChoice');
$table->timestamps();
});
}
<?php
Schema::create('participants', function (Blueprint $table) {
$table->id();
$table->foreign('idRootInfo')->references('id')->on('RootInformation')->onDelete('cascade');
$table->string('nameParticipant');
$table->boolean('genderParticipant');
$table->date('dobParticipant');
$table->text('addressParticipant');
$table->string('phoneNumberParticipant');
$table->string('emailParticipant');
$table->string('instituteNameParticipant');
$table->string('pickupParticipant');
$table->foreignId('idClassChoice');
$table->timestamps();
});
}
@mvenghaus
mvenghaus
mvenghaus3mo ago
does this work? .. shouldn't it be "root_information" .. can you see that the the foreign key is created properly in your db tool ?
No description
mvenghaus
mvenghaus3mo ago
because it's not filament or laravel model methods who delete's the data .. it's the database