F
Filament14mo ago
Jacob

Is it possible to add multiple items to a repeater?

With an action button for example.
32 Replies
toeknee
toeknee14mo ago
You should be able to do that with a mountUsing() and populate it that way
Adnan Yalahow
Adnan Yalahow13mo ago
could you please show how to do it i really need this
toeknee
toeknee13mo ago
Can you explain your use case exactly on what type, as default or as edit etc
Adnan Yalahow
Adnan Yalahow13mo ago
i just need to populate repeater based on select item for example i have products and warehouse if chose warehouse i want related products to be passed into my repeater may be with an action button if possible
toeknee
toeknee13mo ago
So that's just stateupdated on the select item
->afterStateUpdated(fn($state,$set) => $set('my_repeater', MyModel::where('id', $state)->pluck('id', 'name'))
->afterStateUpdated(fn($state,$set) => $set('my_repeater', MyModel::where('id', $state)->pluck('id', 'name'))
Adnan Yalahow
Adnan Yalahow13mo ago
the repeater is getting the data but i want to show the list here is my code
Forms\Components\Select::make('mark_id')
->label('Mark')
->reactive()
->disabledOn('edit')
->relationship('mark', 'name')
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
$wareHouseProducts = WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->get();
$set('products', $wareHouseProducts);
})
->options(function () {
return Mark::whereHas('warehouse', function ($query) {
$query->where('status', 'delivered')
->whereHas('products', function ($query) {
$query->where('remaining_qty', '>', 0);
});
})->pluck('name', 'id');
})->searchable()->reactive(),


Forms\Components\Repeater::make('products')
->columnSpan('full')
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
// dd($state);


[ Forms\Components\Placeholder::make('product_name')
->content($state->product_name)
->label('Product')];


}
}),
Forms\Components\Select::make('mark_id')
->label('Mark')
->reactive()
->disabledOn('edit')
->relationship('mark', 'name')
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
$wareHouseProducts = WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->get();
$set('products', $wareHouseProducts);
})
->options(function () {
return Mark::whereHas('warehouse', function ($query) {
$query->where('status', 'delivered')
->whereHas('products', function ($query) {
$query->where('remaining_qty', '>', 0);
});
})->pluck('name', 'id');
})->searchable()->reactive(),


Forms\Components\Repeater::make('products')
->columnSpan('full')
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
// dd($state);


[ Forms\Components\Placeholder::make('product_name')
->content($state->product_name)
->label('Product')];


}
}),
toeknee
toeknee13mo ago
What do you mean show the list?
$wareHouseProducts = WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->get();
$wareHouseProducts = WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->get();
should be
$wareHouseProducts = WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->pluck('id', 'title');
$wareHouseProducts = WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->pluck('id', 'title');
assuming you have an ID and a Title within the products
Adnan Yalahow
Adnan Yalahow13mo ago
based on your code there could be list of array with id and a title so just show that with in the repeater
toeknee
toeknee13mo ago
Yes so you use the above, you pass in the array with sub arrays with id and title or id and name etc
Adnan Yalahow
Adnan Yalahow13mo ago
i want to show the product name and quantity in place holder in the repeater after i have used the above code and changed ->pluck('id', 'title'); to ->pluck('product_name', 'quantity'); am getting this screenshot
No description
Adnan Yalahow
Adnan Yalahow13mo ago
this is my repeater
Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(2)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
return [
Forms\Components\Placeholder::make('product_name')
->content($state)
->label('Product'),
];


}
}),
Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(2)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
return [
Forms\Components\Placeholder::make('product_name')
->content($state)
->label('Product'),
];


}
}),
toeknee
toeknee13mo ago
That won't work All I can see is you have a single product name. So actually you want to change that to be:
return [
Forms\Components\Placeholder::make('product_name')
->content($state)
->label('Product'),
Forms\Components\Placeholder::make('quantity')
->content($state)
->label('Qty'),
];
return [
Forms\Components\Placeholder::make('product_name')
->content($state)
->label('Product'),
Forms\Components\Placeholder::make('quantity')
->content($state)
->label('Qty'),
];
Run a DD on:
dd(WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->pluck('id', 'title'));
dd(WarehouseProducts::whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->pluck('id', 'title'));
What does the above give you? Ahn you are filling place holders, one second try:
return [
Forms\Components\TextInput::make('product_name')
->disabled()
->content($state)
->label('Product'),
Forms\Components\TextInput::make('quantity')
->disabled()
->content($state)
->label('Qty'),
];
return [
Forms\Components\TextInput::make('product_name')
->disabled()
->content($state)
->label('Product'),
Forms\Components\TextInput::make('quantity')
->disabled()
->content($state)
->label('Qty'),
];
Adnan Yalahow
Adnan Yalahow13mo ago
this is the dd
No description
Adnan Yalahow
Adnan Yalahow13mo ago
i get this if i try to write twice
No description
Adnan Yalahow
Adnan Yalahow13mo ago
i want each product and its quantity as one item in the repeater
toeknee
toeknee13mo ago
Yeah because pluck is more for a select. So let's adjust it:
WarehouseProducts::select('product_name', 'quantity')->whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->get()->toArray();
WarehouseProducts::select('product_name', 'quantity')->whereHas('warehouse.mark', function ($query) use ($state) {
$query->where('id', $state);
})->where('remaining_qty', '>', 0)->get()->toArray();
That should dd with: [ 'product_name' => 'Mobiles', 'quanity' => 500.000000 ], [ 'product_name' => 'laptops', 'quanity' => 200.00000 ]
Adnan Yalahow
Adnan Yalahow13mo ago
yes i have that array that looks jjust like this but how can i show in the repeater
toeknee
toeknee13mo ago
By using the above that should now pass into the repeater with the above code I adjusted? If you want to use placeholders instead just use: $state['product_name']
Adnan Yalahow
Adnan Yalahow13mo ago
what am i doing wrong its giving me this error "Attempt to read property "product_name" on array "
Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(2)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
return [
Forms\Components\Placeholder::make('product_name')
->content($state->product_name)
->label('Product'),
Forms\Components\Placeholder::make('quantity')
->content($state->quantity)
->label('Product'),
];

}
}),
Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(2)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
return [
Forms\Components\Placeholder::make('product_name')
->content($state->product_name)
->label('Product'),
Forms\Components\Placeholder::make('quantity')
->content($state->quantity)
->label('Product'),
];

}
}),
toeknee
toeknee13mo ago
Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(2)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
return [
Forms\Components\Placeholder::make('product_name')
->content($state['product_name'])
->label('Product'),
Forms\Components\Placeholder::make('quantity')
->content($state['quantity'])
->label('Product'),
];

}
}),
Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(2)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
}else{
return [
Forms\Components\Placeholder::make('product_name')
->content($state['product_name'])
->label('Product'),
Forms\Components\Placeholder::make('quantity')
->content($state['quantity'])
->label('Product'),
];

}
}),
Sorry I\m doing too many things at once, you need to call the array part because you pass in an array not an object like normal for state. should work with the above...
Adnan Yalahow
Adnan Yalahow13mo ago
I am really grateful to your help so far to this stage and ihave been trying really hard but i could figure out on how? i tried to use loop to get each object and may be map function but neither solution did not work. So please find sometime for me to help this for one last time thank you in advance
Adnan Yalahow
Adnan Yalahow13mo ago
this is what i get for using loop or map function to get each object
No description
toeknee
toeknee13mo ago
Ok so looking at that, you are repeating the product throughout the map... Can you provide what code you are using now
Adnan Yalahow
Adnan Yalahow13mo ago
schema([

Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(3)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
} else {
return collect($state)->map(function ($product) {
return Forms\Components\TextInput::make('product_name')
->default($product['product_name'])
->label('Product');
})->toArray();
}
})
]);
schema([

Forms\Components\Repeater::make('products')
->columnSpan('full')->columns(3)
->schema(function (\Filament\Forms\Get $get, $state) {
if (!$get('mark_id')) {
return [Forms\Components\Placeholder::make('Select Mark First')];
} else {
return collect($state)->map(function ($product) {
return Forms\Components\TextInput::make('product_name')
->default($product['product_name'])
->label('Product');
})->toArray();
}
})
]);
toeknee
toeknee13mo ago
Can you provide whats about the schema? As you are returning the schema for the repeater has been repeated 3 times as if it's got 3 rows of data from somewhere?
Adnan Yalahow
Adnan Yalahow13mo ago
here is the gist
Adnan Yalahow
Adnan Yalahow13mo ago
Gist
LCLJobsResource.php
GitHub Gist: instantly share code, notes, and snippets.
toeknee
toeknee13mo ago
Do you have a releationship on your LCLJobs Model for 'products' ?
Adnan Yalahow
Adnan Yalahow13mo ago
yes i have
toeknee
toeknee13mo ago
That's why then 😉 so the products are being loaded in the repeater way! the correct way so lets try this:
Forms\Components\Repeater::make('products')
->columnSpan('full')
->columns(3)
->schema([
Forms\Components\TextInput::make('product_name')
->disabled()
->label('Product')
])
Forms\Components\Repeater::make('products')
->columnSpan('full')
->columns(3)
->schema([
Forms\Components\TextInput::make('product_name')
->disabled()
->label('Product')
])
That should then load the product_name of each item returned from the relationship
Adnan Yalahow
Adnan Yalahow13mo ago
ohh thank you so much i do not know how i should appreciate this its been weeks since i have been trying hard thank you once again
toeknee
toeknee13mo ago
Absolutely no problem
Want results from more Discord servers?
Add your server