Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some repeater values disappeared after save #15626

Closed
silverhand7 opened this issue Feb 18, 2025 · 1 comment
Closed

some repeater values disappeared after save #15626

silverhand7 opened this issue Feb 18, 2025 · 1 comment
Labels

Comments

@silverhand7
Copy link

silverhand7 commented Feb 18, 2025

Package

filament/filament

Package Version

v3.2

Laravel Version

v11

Livewire Version

v3

PHP Version

PHP 8.2

Problem description

Please check this video below; some of the values on the repeater field were gone after saving. In the video, I'm using table repeater by awcodes, but the behavior is the same using a regular repeater from filament.

https://drive.google.com/file/d/1rGgK6WKkoDUa3LxGKgwDXvvw6rS2T3iw/view?usp=sharing

  1. If I add two or more values to the repeater, some of them suddenly disappear when I save them, even though the values exist on the database.
  2. If I hard refresh the page, the missing values are there.
  3. If I hit save twice, the disappearing values are gone forever.

Here's my relationship:
User model

  public function organizations(): BelongsToMany
  {
      return $this->belongsToMany(Organization::class)
          ->withPivot('role')
          ->withTimestamps();
  }

  public function organizationUsers(): HasMany
  {
      return $this->hasMany(OrganizationUser::class);
  }

Organization model

public function users(): BelongsToMany
{
    return $this->belongsToMany(User::class)
        ->using(OrganizationUser::class)
        ->withTimestamps();
}

public function organizationUsers(): HasMany
{
    return $this->hasMany(OrganizationUser::class);
}

OrganizationUser model

 protected $table = 'organization_user';

protected $fillable = [
    'organization_id',
    'user_id',
    'role',
];

public function organization(): BelongsTo
{
    return $this->belongsTo(Organization::class);
}

public function user(): BelongsTo
{
    return $this->belongsTo(User::class);
}

Here's the repeater field:

 Repeater::make('organizationUsers')
  ->label('Organizations')
  ->relationship()
  ->schema([
    Forms\Components\Select::make('organization_id')
        ->options(Organization::orderBy('name')->pluck('name', 'id'))
        ->preload()
        ->searchable()
        ->disableOptionsWhenSelectedInSiblingRepeaterItems(),
    Forms\Components\TextInput::make('role')
        ->maxLength(255),
])

it works if I'm using the following code:

->schema([
    Forms\Components\Select::make('organization_id')
        ->options(Organization::orderBy('name')->pluck('name', 'id'))
        ->preload()
        ->searchable()
        ->disableOptionsWhenSelectedInSiblingRepeaterItems(),
    Forms\Components\TextInput::make('role')
        ->maxLength(255),
])
->saveRelationshipsUsing(function (User $record, array $state) {
    $record->organizations()->sync($state);
})

Expected behavior

The repeater values should not be gone after save.

Steps to reproduce

  1. Create has many relationships with pivot table
  2. Create a regular repeater field with the relationship to the pivot table.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/silverhand7/previsible-crm

Relevant log output

@silverhand7
Copy link
Author

silverhand7 commented Feb 18, 2025

@danharrin It turns out it's not a bug; I need to set the incrementing to true when extending the pivot model.

class OrganizationUser extends Pivot
{
    public $incrementing = true;

Would be nice to add this to the Filament documentation.

@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant