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

igor1523's avatar

Livewire ignoring stored collection in component and reloading it in the view #8091

Hi! I have a component that load a bunch of records and then show them in the view.

But besides my collection is right filtered and I did a bunch of tests with dd() to see what was showing, in the view it is not real.

In the component and in the first dump in the view the records are okay, but inside the loop, seems that livewire is making a new relationship request without filter, so it its not using the $design collection in the component and yet making another one that haven't productOrders filtered.

Component:

class TshirtDesignProduction extends BaseComponent
{
    public $design;
    public function mount($designId)
    {

Query filtering

        $this->design = ProductDesign::with([
            'productOrders' => function ($query) {
                $query->whereIn('production_status_id', [1, 29, 34])
                    ->whereHas('product', function ($query) {
                        $query->whereHas('productBlueprint', function ($query) {
                            $query->where('id', 11);
                        });
                    });
            }
        ])
            ->find($designId);
    }

    #[On('renderParent')]
    public function render()
    {
        // dump($this->design->productOrders->pluck('production_status_id')->toArray());

        return view('livewire.tshirt-design-production');
    }
} 

BLADE VIEW In this dump the productOrders are right filtered

<div class="row">
        @php
            dump($design->productOrders->pluck('production_status_id')->toArray());
        @endphp
        <div class="col-auto">
            <img src="{{ "https://static.doppelstore.com.br/catalogo/$design->slug/arte-papelaria.jpg" }}"
                class ="rounded" style="max-width: 200px" alt="">
        </div>
        <div class="col mw-1000">
            <div class="d-flex inline p-2 gap-3 fs-3 bg-light">
                {{ $design->label }}
                @php
                    $designId = $design->id;
                    $key1 = now() . $designId;
                @endphp
                <span>

                    <button type="button" class="btn btn-secondary" title="Dias decorridos"
                        @if ($design->getPriority()) style="background-color:#d771ff!important" @endif>
                        <i class="bi bi-clock-history"></i>
                        <div wire:loading class="spinner-border text-light spinner-border-sm">
                        </div>
                        <span wire:loading.remove class="text-light">
                            {{ $design->getOldestTshirtElapsedDaysFromCreation() }}
                        </span>
                    </button>

                    <button type="button" class="btn btn-secondary" title="Total para produzir">
                        <i class="bi bi-archive"></i>
                        <div wire:loading class="spinner-border text-light spinner-border-sm"></div>
                        <span wire:loading.remove class="text-light">
                            {{ $design->totalTshirtsInProduction() }}
                        </span>

                    </button>

                    <button type="button" class="btn bg-custom-open" title="Em aberto">
                        <i class="bi bi-archive"></i>
                        <div wire:loading class="spinner-border text-light spinner-border-sm">
                        </div>
                        <span wire:loading.remove>
                            {{ $design->tshirtsOpen() }}
                        </span>
                    </button>

                    <button type="button" class="btn btn-info" title="Separado" wire:click="massUpdateToSeparated">
                        <i class="bi bi-archive"></i>
                        <div wire:loading class="spinner-border text-light spinner-border-sm">
                        </div>
                        <span wire:loading.remove>
                            {{ $design->tshirtsSeparated() }}
                        </span>

                    </button>

                    <button type="button" class="btn bg-custom-produced " title="Embalado"
                        wire:click="massUpdateToInExpedition">
                        <i class="bi bi-archive"></i>
                        <div wire:loading class="spinner-border text-light spinner-border-sm"></div>
                        <span wire:loading.remove>
                            {{ $design->tshirtsInExpedition() }}
                        </span>

                    </button> </span>
            </div>

            <div class="row">
                <div class="col">
                    <table class="table col mw-500">

In this dump the productOrders are not right filtered. Seems that it is bringin all productOrders from design, not just the ones that I've filtered in the component. So, for this, my entire loop happens with all records, not just the ones I want.

                        @php
                            dump($design->productOrders->pluck('production_status_id')->toArray());
                        @endphp
                        @foreach ($design->productOrders as $productOrder)
                            @if (Str::contains($productOrder->product->slug, 'camiseta'))
                                @php
                                    $productOrderId = $productOrder->id;
                                    $key2 = now() . $productOrderId;
                                @endphp
                                <livewire:tshirt-production-row :productOrder="$productOrder" :key="$key2" />
                            @endif
                        @endforeach
                    </table>
                </div>

                <div class="col">
                    <table class="table col mw-500">
                        @foreach ($design->productOrders as $productOrder)
                            @if (Str::contains($productOrder->product->slug, 'baby'))
                                @php
                                    $productOrderId = $productOrder->id;
                                    $key3 = now() . $productOrderId;
                                @endphp
                                <livewire:tshirt-production-row :productOrder="$productOrder" :key="$key3" />
                            @endif
                        @endforeach
                    </table>
                </div>
            </div>
        </div>
    </div>

What could be? Thanks!

0 likes
0 replies

Please or to participate in this conversation.