F
Filament15mo ago
gon.exe

Avoid the creation of the same record from a relationship

Hello everyone, How can I avoid the creation of the same record from a relationship if it has not changed? In this way, prevent the same record from being generated repeatedly in the Edit functionalities. It would be desirable to only delete the modified records (and create new records with the new values), delete those deleted records and create the new values. That is, if there was no modification, do not delete the records and create them again. Thank you very much in advance!
No description
14 Replies
gon.exe
gon.exeOP14mo ago
Work Order:
public function ProfileBusinessPartners()
{
return $this->hasMany(WorkOrderOrgStructure::class);
}
public function ProfileBusinessPartners()
{
return $this->hasMany(WorkOrderOrgStructure::class);
}
Work Order Org Structure:
class WorkOrderOrgStructure extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
'amount_per_hour',
'quantity',
'change_price_type',
'change_price_value',
'final_price',
'org_structure_id',
'work_order_id',
'business_partner_id',
];

public function OrgStructure()
{
return $this->belongsTo(OrgStructure::class);
}

public function WorkOrder()
{
return $this->belongsTo(WorkOrder::class);
}

public function BusinessPartner()
{
return $this->belongsTo(BusinessPartner::class);
}

}
class WorkOrderOrgStructure extends Model
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
'amount_per_hour',
'quantity',
'change_price_type',
'change_price_value',
'final_price',
'org_structure_id',
'work_order_id',
'business_partner_id',
];

public function OrgStructure()
{
return $this->belongsTo(OrgStructure::class);
}

public function WorkOrder()
{
return $this->belongsTo(WorkOrder::class);
}

public function BusinessPartner()
{
return $this->belongsTo(BusinessPartner::class);
}

}
Work Order Resource:
TableRepeater::make('TaskOrgStructures')
->label(__('Org Structures'))
->disableLabel()
->relationship('ProfileBusinessPartners')
TableRepeater::make('TaskOrgStructures')
->label(__('Org Structures'))
->disableLabel()
->relationship('ProfileBusinessPartners')
} Does anyone have any ideas? Does anyone have any ideas? Thanks in advance !
Jordy
Jordy14mo ago
not quite a filament question I suppose.. you could use an observer and on creation of said record check if there is already one with simular attributes.. or forcing certain columns to be unique is enough in most cases
gon.exe
gon.exeOP14mo ago
Yes, I assume that it is a filament functionality since I use the "relationship" functionality to insert records into the "Work Order Org Structure" table from the "Work Order" resource. I am going to try to manipulate the creation of records in the related tables but in my system I have many relationships and this would mean having to manually manipulate the creation of each of them
Jordy
Jordy14mo ago
I dont see the issue then? the screenshot you provided has no duplicate records(one is soft deleted), filaments create action creates a records.. the edit action edits its.. nothing different than updating/creating it yourself?
gon.exe
gon.exeOP14mo ago
The issue is that NO modifications are being made to the records. A record is being soft deleted and recreated again. This is giving me serious database sizing problems.
Jordy
Jordy14mo ago
code?
Jordy
Jordy14mo ago
besides this beeing a mess, this is not the action that is beeing ran.. are you using a createaction or just an action, did you change the create/update in the resource? also using raw queries is a nono with eloquent imo but I aint gonna read alat specify your question.. I cant read your mind and dont know your code
gon.exe
gon.exeOP14mo ago
That is the code in my resource. then I DO NOT modify the insert in the related table since it is being resolved by the ">relationship('ProfileBusinessPartners')" functionality in the TableRepeater The problem is as expressed above, when submitting the form, the "relationship()" functionality is reinserting a record that was NOT modified. It marks the existing record as soft deleted and generates its insertion again.
Jordy
Jordy14mo ago
why not use a relation manager instead?
gon.exe
gon.exeOP14mo ago
I am using TableRepeater to improve the user experience. The form has several steps and one of them is to select the necessary resources for the Work Order that is creating
No description
Jordy
Jordy14mo ago
it would be helpful to figure out when this occurs as I can use a repeater without any issues.. same goes with a relation manager
gon.exe
gon.exeOP14mo ago
What other information could I provide you?
Jordy
Jordy14mo ago
Pinpoint the problem, when does this occur? Is it with every model or just this one.. Etc

Did you find this page helpful?