我有两个带电部件
首先我有一个foreach循环
<ul>
@foreach ($sectors as $sector)
<li>{{ $sector->name }} <input type="submit" wire:click="showParent({{ $sector->id }})" class="btn btn-sm btn-primary" value="Adicionar"> </li>
<ul class="mt-2">
@foreach ($sector->children as $child)
<li>{{ $child->name }}</li>
@endforeach
</ul>
@endforeach
</ul>
此循环显示扇区和父扇区,它们有一条连线:单击以将数据传递给事件侦听器。 效果很好。
<?php
namespace App\Http\Livewire;
use App\Sector;
use Livewire\Component;
class SectorList extends Component
{
public $construction;
public $parent;
public function mount($construction)
{
$this->construction = $construction;
}
public function showParent($id){
$parent = Sector::find($id);
$this->parent = $parent;
$this->emit('newParent', $parent->id);
}
public function render()
{
$sectors = Sector::where('construction_id', $this->construction)->WhereNull('parent_id')->with('children')->get();
return view('livewire.sector-list', compact('sectors'));
}
}
第二部分
<?php
namespace App\Http\Livewire;
use App\Sector;
use Livewire\Component;
class Sectors extends Component
{
public $sector_name;
public $parent_id = '';
public $construction;
public $segment;
protected $listeners = ['newParent' => 'submit'];
public function mount($construction)
{
$this->construction = $construction;
}
public function submit($id)
{
$this->parent_id = $id;
//dd($this->parent_id = $id);
$data = [
'name' => $this->sector_name,
'parent_id' => $this->parent_id = $id,
'construction_id' => $this->construction,
//'segment_id' => $this->segment
];
Sector::Create($data);
}
public function render()
{
return view('livewire.sectors');
}
}
这里的问题是,当我提交父ID时,将其另存为null,当我双击wire:click =“ showParent({{$ sector-> id}}))”时,它的提交名称就没有了
<form wire:submit.prevent="submit">
<input type="hidden" wire:model="construction" name="construction_id" value="{{ $construction }}">
<div class="form-group">
<label for="name">{{ trans('cruds.sector.fields.name') }}</label>
<input name="sector_name" id="sector_name" class="form-control" wire:model="sector_name">
<span class="help-block">{{ trans('cruds.sector.fields.name_helper') }}</span>
</div>
<button type="submit" class="btn btn-primary">Salvar</button>
</form>
``````
`````
Can someone help??
Thanks
答案 0 :(得分:0)
问题出在 SectorList 组件控制器中,您不需要将变量 $sectors 作为视图参数传递,而是将其作为 public 放置在类中并通过这种方式在 render 方法中声明值。
public function render()
{
$this->sectors = Sector::where('construction_id', $this->construction)->WhereNull('parent_id')->with('children')->get();
return view('livewire.sector-list');
}