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

Pivot column value in relation manager table missing after allowDuplicates() #14905

Open
huiyang opened this issue Nov 28, 2024 · 1 comment
Open
Labels
Milestone

Comments

@huiyang
Copy link

huiyang commented Nov 28, 2024

Package

filament/filament

Package Version

3.2.122

Laravel Version

11.30.0

Livewire Version

3.5.12

PHP Version

8.2.18

Problem description

relation manager table pivot column value missing after add allowDuplicates() for table in relation manager

Expected behavior

relation manager table should show pivot column value normally

Steps to reproduce

  • create a many to many relation for 2 models
  • add any additional pivot column for the pivot table
  • add pivot columns to both models using method withPivot()
  • create relation manager for 1 of the resource
  • add pivot column to the relation manager table
  • add a new record in relation manager with pivot column value
  • pivot column should show value
  • add allowDuplicates() for relation manager table
  • now the pivot column won't show the value anymore

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

https://github.com/huiyang/example-app

Relevant log output

No response

Donate 💰 to fund this issue

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar
@huiyang
Copy link
Author

huiyang commented Nov 28, 2024

currently i add these to my relation manager to solve my issue,
i not sure if it have any side effect, but it seem to be working now

    protected function hydratePivotRelationForTableRecords(Collection | Paginator | CursorPaginator $records): Collection | Paginator | CursorPaginator
    {
        $table = $this->getTable();
        $relationship = $table->getRelationship();

        if ($table->getRelationship() instanceof BelongsToMany /*&& ! $table->allowsDuplicates()*/) {
            invade($relationship)->hydratePivotRelation($records->all());
        }

        return $records;
    }

and

return $table
            ->allowDuplicates()
            ->modifyQueryUsing(function($query, $table) {
                /** @var BelongsToMany $relationship */
                $relationship = $table->getRelationship();

                $columns = [
                    $query->getModel()->getTable() . '.*',
                    $relationship->getTable() . '.*',         // reversed the select sequence, moved this line to later
                ];

                // if (! $this->allowsDuplicates()) {
                    $columns = [
                        ...invade($relationship)->aliasedPivotColumns(),
                        ...$columns,
                    ];
                // }

                $query->select($columns);
            })

@danharrin danharrin added this to the v3 milestone Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

No branches or pull requests

2 participants