Replies: 4 comments 5 replies
-
Hi @davbecker, I had this exact problem yesterday. Got help from Dan Harrin on Discord. Turns out I was trying to write to a belongs-to-many relationship which, as you pointed out, will try to create the end model through the pivot table (e.g.
Hope this can help! |
Beta Was this translation helpful? Give feedback.
-
@davbecker I've been running into the same issue as you have, what was your workaround / solution? |
Beta Was this translation helpful? Give feedback.
-
Hi, I faced the same issue, my case is I use repeater on productResource to add product_variant, and it works, so I try adding repeater inside product_variant repeater hoping that fields will attach variant_attribute_value to product_variant, but the repeater trying to add variant_attribute_value record instead I try many workaround including answer for chatGPT, claude AI, and also gemini but none of their solutions work for me So this is my productResource
this is my productVariant model
this is my VariantAttributeValue model
and this is migration for tabel product_variant_attribute_value
So any ideas how to fix it? |
Beta Was this translation helpful? Give feedback.
-
I have the same issue, the repeater cant handle Having almost same structure with
Class Product extends Model{
public function options()
{
return $this->morphToMany(Option::class, 'target_model', 'connected_models', 'target_model_id', 'connected_model_id')
->using(ConnectedModel::class);
}
}
class Option extends Model
{
public function products()
{
return $this->morphedByMany(Product::class, 'connected_model', 'connected_models', 'connected_model_id', 'target_model_id');
}
}
class ConnectedModel extends MorphPivot
{
protected $table = 'connected_models';
protected $fillable = [
'target_model_id',
'target_model_type',
'connected_model_id',
'connected_model_type',
'position',
];
} Have anyone had same problem and any idea how to handle this? |
Beta Was this translation helpful? Give feedback.
-
I've encountered a problem while trying to implement a many-to-many relationship in my Laravel application. Specifically, the issue arises when working with a repeater field. I've solve the issue for me and found a workaround. During the process, I've taken a deeper look into the Code of the Repeater.php and I think there might be room for improvement and I'd like to share my thoughts and experience.
Consider the typical example of a User and Role with a pivot table role_user. When adding a new Role for a User using a repeater field, the code inside Repeater.php seemed to be trying to create a new User entry in the users table, instead of just linking the existing User to the Role in the role_user pivot table.
Moreover, when a relationship between a User and a Role was removed, the User was deleted entirely, instead of just removing the relationship:
Here's the part of the code from Repeater.php I'm referencing:
Laravel provides convenient methods such as attach and detach for managing many-to-many relationships. These methods should be leveraged to handle the relationships properly.
Instead of creating a new $record and trying to save it, it should fetch the existing $record and attach it to the $relationship.
Likewise, when removing records from a pivot table, it should use the detach method instead of delete. The detach method removes the relationship without deleting the model instance.
It's possible that I may have misunderstood the functionality of the Repeater.php code or perhaps the entire concept of handling relationships in Laravel and Filament. I would greatly appreciate your thoughts and suggestions.
Beta Was this translation helpful? Give feedback.
All reactions