首页 > 解决方案 > 我无法使用 Laravel-Livewire 在模态中显示数据

问题描述

我尝试使用 dd() 并且可以看到数据,但是当我尝试从刀片验证 maxPoints 变量中是否有任何值时,我得到空值,与变量名相同。

<?php

namespace App\Http\Livewire;

use App\Tests as Test;
use Livewire\Component;

class Tests extends Component
{
    public $selected_id, $name, $maxPoints, $counter;

    public function render()
    {
        return view('livewire.tests.index', ['tests' => Test::get()]);
    }

    public function edit($id){

        $test = Test::findOrFail($id);

        $this->selected_id = $id;
        $this->name = $test->name;
        $this->maxPoints = $test->maxPoints;
        // dd($this->name);
    }
}

在模态中,我尝试显示数据但我没有得到值,它显示为空。

<div wire:ignore.self class="modal fade" id="editTest" data-backdrop="static" tabindex="-1" role="dialog"
   aria-labelledby="exampleModalLabel" aria-hidden="true">
   <div class="modal-dialog" role="document">
       <div class="modal-content">
           <div class="modal-header">
               <h5 class="modal-title" id="exampleModalLabel">Update</h5>
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                   <span wire:click.prevent="cancel()" aria-hidden="true">×</span>
               </button>
           </div>
           <div class="modal-body">
               <form>
                   <input type="hidden" wire:model="selected_id">
                   <div class="form-group">
                       <label for="name"></label>
                       <input wire:model="name" type="text" class="form-control" id="name"
                           placeholder="Name">
                   </div>
                   <div class="form-group">
                       <label for="email"></label>
                       @if ($maxPoints)
                           <input wire:model="maxPoints" value="{{ $maxPoints }}" type="text" class="form-control"
                               id="email" placeholder="Email">
                       @endif
                   </div>
               </form>
           </div>
           <div class="modal-footer">
               <button type="button" wire:click.prevent="cancel()" class="btn btn-secondary"
                   data-dismiss="modal">Close</button>
               <button type="button" wire:click.prevent="update()" class="btn btn-primary"
                   data-dismiss="modal">Save</button>
           </div>
       </div>
   </div>
</div>

标签: laravellaravel-livewire

解决方案


推荐阅读