我在livewire组件上的复选框出现了多头问题。
我正试着保存在表单的第二张复选框中。此复选框在另一个名为"Garde_type“的表中获取它们的值。复选框"home“保存在我表单的表ID 1,复选框”访问“保存ID 2。
拉勒维尔直播要求我的财产规则保存我的数据。好的!但是,当我输入规则时,我的2个复选框:在我的形式中,复选框不能是checked..when,我会选中它们,它们会立即/取消选中=第一个问题。第二个问题:无论是选中还是取消……每次我提交我的表单时,这两个复选框都会考虑“选中”,然后保存在我的数据库中。
在这里,您可以查看我的代码,我尝试了很多东西,但我已经不知道了。
这是带电组件“控制器”:
class VilleSelect extends Component {
public $visit;
public $home;
public $user_id;
protected function rules() {
return [
'visit' => 'nullable',
'home' => 'nullable',
];
}
public function submit() {
$annonces=annonces::create([
'home' => $this->home->id,
'visit' => $this->visit->id,
]);
$annonces->save();
}以下是复选框:
<div class="mt-4 space-y-4">
<div class="flex items-start">
<div class="flex h-5 items-center">
<x-jet-input wire:model="home" value="{{$home->id}}" type="checkbox"/>
</div>
<div class="ml-3 text-sm">
<x-jet-label for="home" value="{{$home->garde_type}}"/>
</div>
</div>
<div class="flex items-start">
<div class="flex h-5 items-center">
<x-jet-input wire:model='visit' value="{{$visit->id}}" type="checkbox"/>
</div>
<div class="ml-3 text-sm">
<x-jet-label for="visit" value="{{$visit->garde_type}}"/>
</div>
</div>
</div>发布于 2022-10-26 05:11:06
在调用属性时,没有理由具体地调用id,因为您已经设置了主页并访问了ID。
试一试
$annonces=annonces::create([
'home' => $this->home,
'visit' => $this->visit,
]);发布于 2022-10-26 12:06:23
好了,现在我的复选框不再有问题了。但它不能像我想的那样起作用。
这是我的复选框:
<x-jet-input wire:model="home" value="1" type="checkbox"/>
<x-jet-input wire:model="visit" value="2" type="checkbox"/>这里我的控制者:
public $visit;
public $home;
public function submit()
{
$annonces=annonces::create([
'home' => $this->home,
'visit' => $this->visit,
]);
$annonces->save();
}但我不想在表单中传递值"1“和"2”。
我尝试的是:
public function submit()
{
$this->visit = Garde_type::find(2)->id;
$this->home = Garde_type::find(1)->id;
$annonces=annonces::create([
'home' => $this->home,
'visit' => $this->visit,
]);
$annonces->save();
}和表单:
<x-jet-input wire:model="home" value="{{home->id}}" type="checkbox"/>
<x-jet-input wire:model="visit" value="{{visit->id}}" type="checkbox"/>但我试过的都没用。
有什么想法吗?
发布于 2022-10-26 21:39:06
我被你的密码弄糊涂了。如果您已经知道了Garde_type的ID,为什么还要再次查找它?
例如,将值设置为ID,然后在Garde_type中搜索您已经拥有的ID,这是多余的。
// Set the value with the variable $home, $visit not just home and visit.
<x-jet-input wire:model="home" value="{{ $home->id }}" type="checkbox"/>
<x-jet-input wire:model="visit" value="{{ $visit->id }}" type="checkbox"/>既然您已经有了Garde_type,为什么要在这里获取它的ID呢?
// The ID will be 2 since you're searching for a record with the ID of 2
$this->visit = Garde_type::find(2)->id;
// The ID will be 1 since you're searching for a record with the ID of 1
$this->home = Garde_type::find(1)->id;你到底想做什么?
https://stackoverflow.com/questions/74193524
复制相似问题