Error using $rowLoop
I previously asked how to number each row and was directed to use $rowLoop in the documentation.
I have use the example in the documentation and get the following error:
get_class(): Argument #1 ($object) must be of type object, int given
line 43: ($livewire->getTableRecordsPerPage * (
If I change getTableRecordsPerPage it works, but if using the excel export plugin:
Undefined property: stdClass::$iteration
line 61: $rowLoop->iteration +
Is this a bug?
9 Replies
Posting the code for your action/etc will probably make it easier to help.
Also, to be clear, are you selecting records on-screen and asking those selected records to be exported? Or are you dumping the whole table to Excel?
Please also read #✅┊rules
Sorry about that.
This is right from the example on a fresh resource that I just created with one relationship.
This is the only column other than the ID.
TextColumn::make('index')->getStateUsing(
static function (stdClass $rowLoop, HasTable $livewire): string {
return (string) (
$rowLoop->iteration +
($livewire->getTableRecordsPerPage * (
$livewire->page - 1
))
);
}
),
Please really read #✅┊rules if you want people to help you 😉
Sorry Dennis...
https://flareapp.io/share/W7zDkRzm
Flare
App\Filament\Admin\Resources\PromotionOpsDCResource::App\Filament\Admin\Resources{closure}(): Argument #2 ($livewire) must be of type App\Filament\Admin\Resources\HasTable, App\Filament\Admin\Resources\PromotionOpsDCResource\Pages\ListPromotionOpsDCs given, called in /home/web/web/projects/public_html/filapanel/pfa/vendor/filament/support/src/C...
Your error message says: "...Argument #2 ($livewire) must be of type App\Filament\Admin\Resources\HasTable ..." for
static function (stdClass $rowLoop, HasTable $livewire): string {
Why is it referring to App\Filament\Admin\Resources\HasTable? I'm guessing you haven't imported the HasTable contract?
(This is where having an IDE helps you when working with code/packages that are new to you.)
This is right from the example on a fresh resource that I just created with one relationship.You can see in the example you copied from the docs that there's a
use
statement in the header showing which contract file to import, but which you haven't included in your code.
And, I think you'll need to change $livewire->page
to $livewire->paginators['page']
You can dd($livewire)
inside your closure to inspect more of its properties.
Edit:
Also be aware that your current implementation may give undesired results if you allow column-sorting in your table, because the numbers won't stay with the reordered records. If you're not going to allow sorting, that may be fine. Just something to consider.Thank you. I did know about HasTable, I had omitted it from the file while I was testing.
I also elimited order.
I believe I have it all working. There is one odd behaviour. It gives an error about $livewire->getTableRecordsPerPage not being an int when "all" is selected.
I have a workaround, although pretty crude...I'm sure there is a much cleaner way to do this, unless I am doing something wrong altogether here and a fix is not needed? 😕 😕
static function (stdClass $rowLoop, HasTable $livewire): string { if($livewire->tableRecordsPerPage == 'all') { $tableRecordsPerPage = 9999; } else { $tableRecordsPerPage = $livewire->tableRecordsPerPage; } return (string) ($rowLoop->iteration + ($tableRecordsPerPage * ($livewire->paginators['page'] - 1 )) ); }
static function (stdClass $rowLoop, HasTable $livewire): string { if($livewire->tableRecordsPerPage == 'all') { $tableRecordsPerPage = 9999; } else { $tableRecordsPerPage = $livewire->tableRecordsPerPage; } return (string) ($rowLoop->iteration + ($tableRecordsPerPage * ($livewire->paginators['page'] - 1 )) ); }
I guess you could just set it to 1 if it's set to All, since you're multiplying by the per-page value.
I believe ‘all’ should be -1