foreach() 方法在关闭模式后检索 null - Livewire

foreach() method retrieves null after closing modal - Livewire

我正在 laravel 8 上使用 livewire,我有一个包含 table、一些按钮和一组模态的组件,用于对 [=41] 中的日期进行一些操作=]秒。有一种模式包含三个 @Foreach 循环,用于三个 select 输入字段,其值是从组件的控制器中获取的。当我打开同一组件上描述的任何其他模式时,它一直给我以下错误:

foreach() argument must be of type array|object, null given (View: C:\Servers\xampp\htdocs\region-admin\resources\views\livewire\users.blade.php)

指的是该模态上的三个 @Foreach 循环。

我尝试刷新组件中的值,但没有任何答案。但是,在打开该模式之前不应加载这些值。

这里的元素:

具有三个@foreach 循环的模式

{{-- Modal equipo de liderazgo --}}
    <x-jet-dialog-modal wire:model="usuario_modal_liderazgo_visible">
        <x-slot name="title">
        {{ __('Equipo de Liderazgo') }}
        </x-slot>

        <x-slot name="content">
        {{ __('Seleccione a los miembros que conforman el equipo de liderazgo para el capítulo') }}

            <div class="m-1">
                <!-- Campo presidente -->
                <div class="inline-flex w-full items-center">
                    <x-label for="presidente" class="w-1/3" value="{{ __('Presidente: ') }}" />
                    <select name="presidente" id="presidente" class="mt-1 block w-full" wire:model.defer="presidente">
                        <option value="0">{{ __(' No asignado ') }}</option>
                        @foreach ($usuarios as $usuario)
                            <option value="{{ $usuario['id']}}">{{ $usuario['nombre'] . " " . $usuario['a_paterno'] . " " . $usuario['a_materno']}}</option>
                        @endforeach
                    </select>
                </div>

                <!-- Campo vicepresidente -->
                <div class="inline-flex w-full items-center">
                    <x-label for="vicepresidente" class="w-1/3" value="{{ __('Vicepresidente: ') }}" />
                    <select name="vicepresidente" id="vicepresidente" class="mt-1 block w-full" wire:model.defer="vicepresidente">
                        <option value="0">{{ __(' No asignado ') }}</option>
                        @foreach ($usuarios as $usuario)
                            <option value="{{ $usuario['id']}}">{{ $usuario['nombre'] . " " . $usuario['a_paterno'] . " " . $usuario['a_materno']}}</option>
                        @endforeach
                    </select>
                </div>

                <!-- Campo Secretario Tesorero -->
                <div class="inline-flex w-full items-center">
                    <x-label for="s_tesorero" class="w-1/3" value="{{ __('Secretario Tesorero: ') }}" />
                    <select name="s_tesorero" id="s_tesorero" class="mt-1 block w-full" wire:model.defer="s_tesorero">
                        <option value="0">{{ __(' No asignado ') }}</option>
                        @foreach ($usuarios as $usuario)
                            <option value="{{ $usuario['id']}}">{{ $usuario['nombre'] . " " . $usuario['a_paterno'] . " " . $usuario['a_materno']}}</option>
                        @endforeach
                    </select>
                </div>


            </div>

        </x-slot>

        <x-slot name="footer">
            <x-jet-secondary-button wire:click="$toggle('usuario_modal_liderazgo_visible')" wire:loading.attr="disabled">
            {{ __('Cancelar') }}
            </x-jet-secondary-button>

            <x-jet-danger-button class="ml-2" wire:click="equipoLiderazgo" wire:loading.attr="disabled">
            {{ __('Actualizar Equipo de Liderazgo') }}
            </x-jet-danger-button>
        </x-slot>

    </x-jet-dialog-modal>

它产生的错误:

Facade\Ignition\Exceptions\ViewException: foreach() argument must be of type array|object, null given (View: C:\Servers\xampp\htdocs\region-admin\resources\views\livewire\users.blade.php) in file C:\Servers\xampp\htdocs\region-admin\resources\views/livewire/users.blade.php on line 1107

#0 C:\Servers\xampp\htdocs\region-admin\resources\views/livewire/users.blade.php(1107): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'foreach() argum...', 'C:\Servers\xamp...', 1107)
#1 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\ComponentConcerns\RendersLivewireComponents.php(50): include('C:\Servers\xamp...')
#2 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\ComponentConcerns\RendersLivewireComponents.php(51): Livewire\CompilerEngineForIgnition->Livewire\ComponentConcerns\{closure}()
#3 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\View\Engines\CompilerEngine.php(61): Livewire\CompilerEngineForIgnition->evaluatePath('C:\Servers\xamp...', Array)
#4 C:\Servers\xampp\htdocs\region-admin\vendor\facade\ignition\src\Views\Engines\CompilerEngine.php(37): Illuminate\View\Engines\CompilerEngine->get('C:\Servers\xamp...', Array)
#5 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\View\View.php(139): Facade\Ignition\Views\Engines\CompilerEngine->get('C:\Servers\xamp...', Array)
#6 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\View\View.php(122): Illuminate\View\View->getContents()
#7 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\View\View.php(91): Illuminate\View\View->renderContents()
#8 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\Component.php(173): Illuminate\View\View->render()
#9 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\HydrationMiddleware\RenderView.php(14): Livewire\Component->output()
#10 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\LifecycleManager.php(133): Livewire\HydrationMiddleware\RenderView::dehydrate(Object(App\Http\Livewire\Users), Object(Livewire\Response))
#11 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\Connection\ConnectionHandler.php(14): Livewire\LifecycleManager->dehydrate()
#12 C:\Servers\xampp\htdocs\region-admin\vendor\livewire\livewire\src\Controllers\HttpConnectionHandler.php(13): Livewire\Connection\ConnectionHandler->handle(Array)
#13 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php(48): Livewire\Controllers\HttpConnectionHandler->__invoke('users')
#14 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Route.php(254): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Livewire\Controllers\HttpConnectionHandler), '__invoke')
#15 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Route.php(197): Illuminate\Routing\Route->runController()
#16 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(692): Illuminate\Routing\Route->run()
#17 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#19 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#21 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#22 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#24 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Session\Middleware\AuthenticateSession.php(58): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#25 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Session\Middleware\AuthenticateSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(121): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#27 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php(63): Illuminate\Session\Middleware\StartSession->handleStatefulRequest(Object(Illuminate\Http\Request), Object(Illuminate\Session\Store), Object(Closure))
#28 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php(67): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#33 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#34 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(694): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#35 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(669): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#36 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(635): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#37 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(624): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#38 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(166): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#39 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#40 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#42 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#43 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 C:\Servers\xampp\htdocs\region-admin\vendor\fruitcake\laravel-cors\src\HandleCors.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#45 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 C:\Servers\xampp\htdocs\region-admin\vendor\fideloper\proxy\src\TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#47 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#48 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#49 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#50 C:\Servers\xampp\htdocs\region-admin\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#51 C:\Servers\xampp\htdocs\region-admin\public\index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#52 {main}

它是如何工作的一些图片:

table view

Modal opened

First view to the error log

Detailed view to the error log

因此,在 ceejaroz and Qirel 的评论之后,我删除了模态并将其更改为警报,然后将所有 @foreach 循环替换为 @forelse 并且效果很好。