我在 livewire 組件上的復選框有問題。
我正在嘗試在表單的表格中保存兩個復選框。此復選框在另一個名為“Garde_type”的表中取值。復選框“home”在我的表單表中保存 ID 1,復選框“visit”保存 ID 2。這里一切都很簡單......但是
Laravel livewire 詢問我的屬性規則來保存我的資料。好的 !但是,當我輸入規則時,我的 2 個復選框:在我的表單中,無法選中該復選框..當我檢查它們時,它們立即選中/取消選中 = 第一個問題。第二個問題:無論選中/取消選中...每次我提交表單時,這兩個復選框都被視為“選中”,然后保存在我的資料庫中。
在這里你可以看看我的代碼,我嘗試了很多東西,但我不知道了。
這是 livewire 組件“控制器”:
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 hljs-number">3 text-sm">
<x-jet-label for="home" value="{{$home->garde_type}}"/>
</div>
</div>
<div hljs-string">">
<div hljs-number">5 items-center">
<x-jet-input wire:model='visit' value="{{$visit->id}}" type="checkbox"/>
</div>
<div hljs-number">3 text-sm">
<x-jet-label for="visit" value="{{$visit->garde_type}}"/>
</div>
</div>
</div>
uj5u.com熱心網友回復:
呼叫您的屬性時,沒有理由專門呼叫 id,因為您已經回家并訪問了 ID。
嘗試
$annonces=annonces::create([
'home' => $this->home,
'visit' => $this->visit,
]);
uj5u.com熱心網友回復:
好吧,現在我的復選框不再出錯了。但它不會像我想要的那樣作業。
這是我的復選框:
<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"/>
但我嘗試的一切都沒有奏效。
任何想法?
uj5u.com熱心網友回復:
我對你的代碼感到困惑。如果你已經知道 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://www.uj5u.com/qianduan/520519.html
標籤:拉拉维尔调试复选框雄辩laravel-livewire
