filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.59k stars 2.99k forks source link

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

Open huiyang opened 3 days ago

huiyang commented 3 days ago

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

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

Fund with Polar

huiyang commented 2 days ago

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);
            })