I’m having a doble problems with my checkboxes on livewire component.
I’m trying to save in my form’s table two checkboxes. This checkboxes take their value in an other table named "Garde_type".
Checkbox "home" save in my form’s table the ID 1, checkbox "visit" save ID 2.
Here all is simple…but
Laravel livewire ask me property rules to save my data. Ok ! But when i put in rules my 2 checkboxes : in my form the checkbox can’t be checked..when i check them they check/uncheck immediately = first problem.
Second problem : no matter check/uncheck…everytime i submit my form, the 2 checkboxes are consider "checked" and then saved in my DB.
Here you can take a look to my code, i tried many things but i have no idea any more.
This is the livewire component "controler" :
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();
}
This is the checkboxes :
<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>
3
Answers
Well now my checkboxes don't bug any more. But it won't work as i want to.
This is my checkboxes :
Here my controler :
But i don't want to pass the value "1" and "2" in the form.
What i tried is that :
And in the form :
But all i tried doesn't worked.
Any idea?
When calling your properties there is no reason to call the id specifically as you ae setting home and visit to the ID already.
Try
I’m confused by your code. If you already know the ID of the Garde_type, why are you looking for it again?
For instance, setting the value to an ID and then searching your Garde_type for the ID which you already have is redundant.
Why are you trying to get the ID for the Garde_type here, when you already have it?
What are you trying to accomplish exactly?