F
Filament10mo ago
thyk123

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
mvenghaus10mo ago
usually you define this using the foreign key in your database
mvenghaus
mvenghaus10mo 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
mvenghaus10mo ago
->onDelete('cascade');
thyk123
thyk123OP9mo 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
mvenghaus9mo ago
how does your migration file look like?
thyk123
thyk123OP9mo 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
mvenghaus9mo 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
mvenghaus9mo ago
because it's not filament or laravel model methods who delete's the data .. it's the database
Want results from more Discord servers?
Add your server