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

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?
No description
9 Replies
Hugo
Hugo5mo ago
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
]);
}
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
]);
}
Dan Harrin
Dan Harrin5mo ago
have you seen the repeatableentry?
atabegruslan
atabegruslanOP5mo ago
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);
}
public function product_variants()
{
return $this->hasMany(ProductVariant::class);
}
My ProductVariant have a field called name
Dan Harrin
Dan Harrin5mo ago
I think @Hugo means accessing the $infolist->getRecord() and looping through the relationship, which is another option
Hugo
Hugo5mo ago
Yes exactly ! 👍
atabegruslan
atabegruslanOP5mo ago
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'),
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?
Dan Harrin
Dan Harrin5mo ago
indeed, does it work?
atabegruslan
atabegruslanOP5mo ago
let me try it worked, thank you
Want results from more Discord servers?
Add your server