Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SigalZ's avatar

Livewire component's data not refreshed

Using Laravel 10 Livewire 3.

I update a field called stock_weight in the coffees table with a form.

I can see it is calling the render method after the saveShopUsage method but it does not show the new data of the stock_weight field from the coffees table.

If I refresh the page, the data is refreshed.

My component's method:

#[On('save-shop-usage')]
public function saveShopUsage($coffeeId, $weight)
{     
        $coffee = Coffee::find($coffeeId);                
        $stockLeft = $coffee->stock_weight - $weight;
        $coffee->update(['stock_weight' => $stockLeft]);

        $this->alert('success', 'Hurrah!', [
            'position' => 'center',
            'timer' => 1000,
            'toast' => false,
            'text' => 'Shop usage has been updated'
        ]);
 }

 public function render()
  {           
        $this->roastTypes = RoastType::orderBy('name')->get();

        //Site coffees
        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();            

            $coffees = $coffees->toArray();
            $this->siteCoffees[] = ['roast_type' => $type->name, 'coffees' => $coffees];

		//dd($this->siteCoffees) calling this after the saveShopUsage method is called, shows the old data and not 	the new one that is actually in the coffees table

 		    return view('livewire.admin.warehouse.warehouse-component');
  }

My blade:

<div>

<div class="row">
    @if(count($siteCoffees) > 0)
    <div class="col-md-6">
        <div class="card card-secondary">
            <div class="card-header"><h3 class="card-title">Coffees on the Site</h3></div>
            <div class="card-body">
                @foreach($siteCoffees as $mainKey => $val)
                    @if(count($val['coffees']) > 0)
                        <div class="card card-secondary card-outline">
                            <div class="card-header">{{ $val['roast_type'] }}</div>
                            <div class="card-body">
                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>Coffee</th>
                                                <th></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($val['coffees'] as $key => $cof)
                                            <tr wire:key="{{ $cof['id'] }}">
                                                <td>
                                                    {{ $cof['name'] }}<br>
                                                    <label>In Stock:</label> {{ $cof['stock_weight'] }}<br>{{-- shows old data here after saving--}}
                                                </td>
                                                <td>
                                                    <b>Last Roast Date:</b>
                                                    {{ $cof['last_roast_date'] == null ? 'N/A' : $cof['last_roast_date'] }}
                                                    @if($cof['stock_weight'] > 0)
                                                        <p><a href="{{ route('warehouse.pack-bags', $cof['id']) }}" class="btn btn-default btn-sm">Pack Bags</a></p>
                                                        @can('update shop usage')                                                        
                                                        <form class="form shopUsageForm" role="form" method="POST">
                                                            @csrf
                                                            
                                                            <div class="form-group">
                                                                <label>Shop Usage <span style="color:red">(In Grams, Minimum 1g)</span></label>                                                                
                                                                <input type="text" name="shop_usage_weight" class="form-control input-sm" value="{{ $cof['stock_weight'] }}"> {{-- shows whatever ever amount I put here before saving --}}
                                                                <input type="hidden" name="coffee_id" value="{{ $cof['id'] }}">
                                                            </div>
                                                            <button type="submit" class="btn btn-default btn-sm">Save Shop Usage</button>
                                                        </form>
                                                        @endcan
                                                    @endif
                                                </td>                             
                                            </tr>
                                            @endforeach
                                            <tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>
    @endif
</div>

@script
<script>
$('.shopUsageForm').submit(function() {
        var form = $(this);
        var url = $(this).attr('action');
        var data = $(this).serialize();
        var weight = form.find('input[name="shop_usage_weight"]').val();
        var coffeeId = form.find('input[name="coffee_id"]').val();

        swal.fire({
           title: "Confirm Allocate to Shop",
           text: 'Allocate ' + weight + 'g to the shop?',
           icon: "warning",
           showCancelButton: true,
           confirmButtonText: "Yes, do it, do it now!",
           cancelButtonText: "Pfff... No, cancel!"
       }).then((result) => {
            if(result.isConfirmed) {
                $wire.dispatch('save-shop-usage', { coffeeId: coffeeId, weight: weight });
            }
        }
       )
       return false;
    });
</script>
@endscript

</div>

Why is the stock_weight in the $siteCoffees['coffees'] does not reflect the new updated number?

0 likes
0 replies

Please or to participate in this conversation.