#How loop a list in View item view (infolist)?

11 messages · Page 1 of 1 (latest)

thin sinew
#

Im facing a situation where a Product can have many Product variants.
So in the view-one-product view, I need to loop through all its product variants and display those variants in a list.
How can I do that?

floral zealotBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

fast oyster
#

You can build a new section dynamically before the “return $infolist”, by doing your foreach on the variants of your product, store this section in a variable and display it in your schema.

Something like that :

public static function infolist(Infolist $infolist): Infolist
    {
        $variantsSection = // build your variantSection with a foreach here
        
        return $infolist
            ->schema([
                Forms\Components\Section::make()
                    ->schema([
                        TextEntry::make('name'),
                        TextEntry::make('category.name'),
                        ImageEntry::make('thumbnail'),
                    ]),

                $variantSection
            ]);
    }
tiny kernel
#

have you seen the repeatableentry?

thin sinew
tiny kernel
#

I think @fast oyster means accessing the $infolist->getRecord() and looping through the relationship, which is another option

thin sinew
#

But I dont see any forloops in there.

So it the loop under the hood, and all I need to do is

    public static function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                Components\Section::make()->schema([
                    Components\TextEntry::make('name'),
                    Components\TextEntry::make('category.name'),
                    Components\ImageEntry::make('thumbnail'),
                ]),
                Components\Section::make()->schema([
                    Components\RepeatableEntry::make('product_variants')
                        ->schema([
                            TextEntry::make('name'),

right?

tiny kernel
#

indeed, does it work?

thin sinew
#

let me try