#How loop a list in View item view (infolist)?
11 messages · Page 1 of 1 (latest)
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
]);
}
have you seen the repeatableentry?
Sorry but that doesnt really help. It just moves the question from 1 place to another.
Can you please give me an example snippet of code, showing the syntax of your // build your variantSection with a foreach here, involving a for loop ...
My Product model have
public function product_variants()
{
return $this->hasMany(ProductVariant::class);
}
My ProductVariant have a field called name
I think @fast oyster means accessing the $infolist->getRecord() and looping through the relationship, which is another option
Yes exactly ! 👍
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?
indeed, does it work?
let me try