Laravel Livewire Typed 属性 在表单提交初始化之前不得访问
Laravel Livewire Typed property must not be accessed before initialization on form submission
livewire 新手,正在尝试将现有表单转换为 livewire 组件。
当我提交表单时,我收到了这个错误:
Typed property App\Http\Livewire\LocationInfo::$location must not be accessed before initialization
我不确定如何进行这项工作,或者我做错了什么。我原以为 save() 方法会知道 $this->location
,但也许我误会了什么。似乎无法在文档中找到与我正在做的有什么不同。我也浏览了其他有同样错误的问题,但没有找到答案。
我可以将 Log 语句移动到 mount 方法或 render 方法,它会记录正确的信息。我不明白为什么它不能用于 save() 方法。
这是我的 livewire 组件:
class LocationInfo extends Component
{
public Object $states;
public Location $location;
protected $rules = [
'location.name' => ['required', 'string', 'max:150'],
...
];
public function mount(Location $location)
{
$this->location = $location;
$this->states = USState::all();
}
public function render()
{
return view('livewire.location-info');
}
public function save()
{
Log::debug($this->location);
}
}
这是我的表格:
<div class="modal fade" id="location_info_modal" tabindex="-1" aria-labelledby="location_info_modal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<form wire:submit.prevent="save" class="" id="location_info_form" method="POST" action="/locations/{{ $location->id }}">
<div class="modal-content">
<div class="modal-header d-flex justify-content-between">
<div id="modal_header" class="d-flex justify-content-start align-items-center">
<h5 class="modal-title pe-3">Edit Location Info</h5>
</div>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="btn-close"></button>
</div>
<div class="modal-body">
<div class="row">
@csrf
<div class="mb-3 col-md-8">
<label for="name" class="form-label">Location Name</label>
<input wire:model="location.name" type="text" class="form-control" id="name" placeholder="name" name="name" value="{{ $location->name ?? '' }}" data-inputmask-regex="[A-Za-z0-9#.,''-() &]+">
@error('location.name') <span class="error">{{ $message }}</span> @enderror
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary save_btn" id="location_info_save" disabled>Save</button>
</div>
</div>
</form>
</div>
</div>
这是我的路线:
Route::post('/locations/{id}', [LocationInfo::class, 'save'])
->middleware('auth');
我之前遇到过这个错误,是 typed property
尝试像这样删除它:
public $location;
public function mount(Location $location)
{
$this->location = $location;
}
或者移除坐骑中的那个:
public Location $location;
public function mount()
{
$this->location = new Location();
}
livewire 新手,正在尝试将现有表单转换为 livewire 组件。
当我提交表单时,我收到了这个错误:
Typed property App\Http\Livewire\LocationInfo::$location must not be accessed before initialization
我不确定如何进行这项工作,或者我做错了什么。我原以为 save() 方法会知道 $this->location
,但也许我误会了什么。似乎无法在文档中找到与我正在做的有什么不同。我也浏览了其他有同样错误的问题,但没有找到答案。
我可以将 Log 语句移动到 mount 方法或 render 方法,它会记录正确的信息。我不明白为什么它不能用于 save() 方法。
这是我的 livewire 组件:
class LocationInfo extends Component
{
public Object $states;
public Location $location;
protected $rules = [
'location.name' => ['required', 'string', 'max:150'],
...
];
public function mount(Location $location)
{
$this->location = $location;
$this->states = USState::all();
}
public function render()
{
return view('livewire.location-info');
}
public function save()
{
Log::debug($this->location);
}
}
这是我的表格:
<div class="modal fade" id="location_info_modal" tabindex="-1" aria-labelledby="location_info_modal" aria-hidden="true">
<div class="modal-dialog modal-lg">
<form wire:submit.prevent="save" class="" id="location_info_form" method="POST" action="/locations/{{ $location->id }}">
<div class="modal-content">
<div class="modal-header d-flex justify-content-between">
<div id="modal_header" class="d-flex justify-content-start align-items-center">
<h5 class="modal-title pe-3">Edit Location Info</h5>
</div>
<button type="button" class="btn-close" data-bs-dismiss="modal"
aria-label="btn-close"></button>
</div>
<div class="modal-body">
<div class="row">
@csrf
<div class="mb-3 col-md-8">
<label for="name" class="form-label">Location Name</label>
<input wire:model="location.name" type="text" class="form-control" id="name" placeholder="name" name="name" value="{{ $location->name ?? '' }}" data-inputmask-regex="[A-Za-z0-9#.,''-() &]+">
@error('location.name') <span class="error">{{ $message }}</span> @enderror
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary save_btn" id="location_info_save" disabled>Save</button>
</div>
</div>
</form>
</div>
</div>
这是我的路线:
Route::post('/locations/{id}', [LocationInfo::class, 'save'])
->middleware('auth');
我之前遇到过这个错误,是 typed property
尝试像这样删除它:
public $location;
public function mount(Location $location)
{
$this->location = $location;
}
或者移除坐骑中的那个:
public Location $location;
public function mount()
{
$this->location = new Location();
}