Hello, using Laravel 10 Livewire 3.
I have a component where I have an array holding data collections.
I need to run on this array on the blade and for each item in a collection have a form and bind the inputs.
A bit hard from me to explain so if you have questions please let me know.
The input fields are not being bind, I'm not sure if it is actually possible to do?
My code:
In the component:
public function mount()
{
$this->roastTypes = RoastType::orderBy('name')->get();
}
public function render()
{
foreach($this->roastTypes as $type) {
$coffees = Coffee::where('active', 1)
->where('blend', 0)
->where('roast_type_id', $type->id)
->where('in_stock', 1)
->orderBy('name')
->get();
$this->siteCoffees[] = ['roast_type' => $type->name, 'coffees' => $coffees];
}
return view('livewire.admin.warehouse.warehouse-component');
}
In the blade (The inputs fields in the form do not show any values):
@foreach($val['coffees'] as $key => $cof)
<tr wire:key="{{ $cof->id }}">
<td>
{{ $cof->name }}<br>
<label>In Stock:</label> {{ $cof->stock_weight }}
</td>
<td>
@can('update shop usage')
<form class="form" role="form" method="POST" wire:submit="shopUsage">
@csrf
<input type="text" wire:model="siteCoffees.{{ $mainKey }}.coffees.{{ $key }}.id">
<div class="form-group">
<label>Shop Usage <span style="color:red">(In Grams, Minimum 1g)</span></label>
<input type="text" wire:model="siteCoffees.{{ $mainKey }}.coffees.{{ $key }}.stock_weight" class="form-control input-sm">
</div>
<button type="submit" class="btn btn-default btn-sm">Save Shop Usage</button>
</form>
@endcan
</td>
```
An example data from the siteCoffees array:
2 => array:2 [▼
"roast_type" => "Med Dark"
"coffees" => Illuminate\Database\Eloquent\Collection {#2052 ▼
#items: array:4 [▼
0 => App\Models\Coffee {#2053 ▼
#connection: "mysql"
#table: "coffees"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#escapeWhenCastingToString: false
#attributes: array:25 [▼
"id" => 4
"name" => "Colombia"
"green_bean_id" => 18
"short_name" => "Colombia"
"slug" => "colombia"
"origin_id" => 1
"coffee_desc" => """
<p><strong><em>Excelso</em></strong> </p>
<p>Medium dark roast.</p>
<p>If you want a single origin with the balanced nature of a blend, this is the one for you. Balanced and smooth with a sweet and slightly fruity flavour. Perfe
▶
"""
"short_desc" => """
<p style="text-align: left;"><strong><em>Excelso </em></strong></p>
<p>Medium dark roast.</p>
<p><span style="font-size: 14px;">If you want a single origin with the balanced nature of a blend, this is the one for you. Balanced and smooth with a sweet and
▶
"""
"blend" => 0
"roast_type_id" => 2
"price_1kg" => "370.00"
"price_250g" => "95.00"
"in_stock" => 1
"stock_weight" => 0
"stock_kg_bags" => 0
"stock_g_bags" => 0
"stock_unbranded_bags" => 0
"last_roast_date" => "2023-10-10"
"new_bean" => "0"
"new_bean_date" => "2022-11-14"
"sticker_id" => 1
"active" => 1
"user_id" => 17
"created_at" => "2017-03-22 15:55:04"
"updated_at" => "2024-03-06 08:27:03"
]
#original: array:25 [▶]
#changes: []
#casts: array:4 [▶]
#classCastCache: []
#attributeCastCache: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
+usesUniqueIds: false
#hidden: []
#visible: []
#fillable: array:22 [▶]
#guarded: array:1 [▶]
}
1 => App\Models\Coffee {#2145 ▶}
For more testing, I tried something simpler as well, doesn't work either, and I added the Validate in case that's the problem:
#[Validate([
'activeCoffees.*.stock_weight' => [
'numeric',
'min:1',
],
])]
public $activeCoffees = [];
public function mount()
{
$this->roastTypes = RoastType::orderBy('name')->get();
$this->activeCoffees = Coffee::where('active', 1)->where('blend', 0)->orderBy('roast_type_id')->orderBy('name')->get();
}
In the blade (I see the values of {{ $key }} and {{ $val->name }} {{ $val->stock_weight }} but nothing in the input field):
@foreach($activeCoffees as $key => $val)
<p><input type="text" wire:model="activeCoffees.{{ $key }}.stock_weight">{{ $key }} {{ $val->name }} {{ $val->stock_weight }}</p>
@endforeach
Thank you