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.28k stars 2.96k forks source link

RepeatableEntry model injection #8923

Closed prazny closed 11 months ago

prazny commented 1 year ago

Package

filament/filament

Package Version

v3.0.68

Laravel Version

v10.26.2

Livewire Version

v3.0.5

PHP Version

PHP 8.1

Problem description

Making RepeatableEntry with Actions is what I'm attempting. I have a model called "Order" that has many "Offers." "Order" models are used in the action functions.

return $infolist
            ->schema([
                Infolists\Components\TextEntry::make('name')->label(__("name")),
                Infolists\Components\RepeatableEntry::make('offers')
                    ->schema([
                        Infolists\Components\TextEntry::make('content')->label(__("content")),
                        Infolists\Components\Actions::make([
                            Actions\Action::make("test")
                                ->url(function (Model $model) {
                                    return "example.com/" . $model->id; // expected Offer but Order is returned
                                })
                        ]),
                    ])
            ]);

Expected behavior

I have expected to have Offer in action function url()/action();

Steps to reproduce

  1. Make two models that are related many to one.
  2. Create a RepeatableEntry with a variety of offers.
  3. Implement action in RepeatableEntry.

Reproduction repository

https://github.com/prazny/repeatable-reproduction

Relevant log output

No response

zepfietje commented 1 year ago

Duplicate of https://github.com/filamentphp/filament/issues/6964?

prazny commented 1 year ago

Duplicate of #6964?

Not sure. If i add code metioned in #6964 I have the same problem but proposed correction by @fallback solves it while my problem still persists.

zepfietje commented 1 year ago

Alright, thanks for checking. It's probably a different bug report I've seen before, or maybe I encountered it myself sometime. Will keep this issue open.

awcodes commented 1 year ago

hey @prazny

this will solve your issue:

Infolists\Components\Actions::make([
  Actions\Action::make("test")
      ->url(function (Model $record, Actions\Action $action) {
          $data = $action
              ->getInfolistComponent()
              ->getContainer()
              ->getParentComponent()
              ->getState();

          return "https://example.com/" . $data['id'];
      })
 ])

@danharrin we could potentially add this as a check on the infolist action with a fallback to the the non repeatable $record, but I don't know if it would be better as a different parameter to inject since using $record might be confusing since the contexts aren't really discernible from the code.

Could potentially be an issue with the Actions container too, since similar behavior being reported with form repeaters involving actions.

danharrin commented 11 months ago

it works just fine if you swap Model $model or Model $record using the original code example, I didn't even need the suggestion from Adam

Maybe a recent update thing