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

TiboriusDev's avatar

foreach array does not update

I save a new entry and then retrieve the data from the database again. But the foreach only shows the number of entries it had before. So there were 5 before and after creating a new entry there are still 5, but the variable has 6 entries. count($bookings) show 6

laravel 11 livewire 3

<ul class="grid grid-cols-1 gap-4">
    {{ count($bookings) }}
    @foreach ( $bookings as $booking)
        <li wire:key="booking-{{ $booking->id }}" class="flex justify-between items-center p-3 bg-slate-100 border-2 border-slate-200 rounded-lg">
            <div class="flex gap-3">
                @if( $booking->type == 'in' )
                    <x-tabler-database-plus class="h-8 w-8 stroke-1-5 text-emerald-400" />
                @else
                    <x-tabler-database-minus class="h-8 w-8 stroke-1-5 text-rose-400" />
                @endif
                <div class="flex flex-col">
                    <span class="text-xs">{{ \Carbon\Carbon::parse($booking->booking_date)->format('d.m.Y') }}</span>
                    <span>{{ $booking->comment }}</span>
                </div>
            </div>
            <div>
                <span class="font-semibold text-lg">{{ ($booking->type == 'out') ? '-' : '' }}{{   number_format($booking->value, 2, ',', '.') }} €</span>
            </div>
    @endforeach
</ul>

0 likes
2 replies
jamesbuch79's avatar
Level 1

There's a missing </li> tag which might be causing rendering issues.

<ul class="grid grid-cols-1 gap-4">
    {{ count($bookings) }}
    @foreach ($bookings as $booking)
        <li wire:key="booking-{{ $booking->id }}" class="flex justify-between items-center p-3 bg-slate-100 border-2 border-slate-200 rounded-lg">
            ... snip ...
        </li>
    @endforeach
</ul>

If that's not it, maybe try resetting properties, or force refreshing with

$this->addCheckoutModal = false;
$this->mount();  // This will re-run the mount method

Or force a fresh query:

$this->bookings = Checkout::where('group_id', '=', $this->group->id)->fresh()->get();

If all else fails, try something like wire:poll techniques.

1 like

Please or to participate in this conversation.