Home > Enterprise >  Bug on checkboxes in laravel livewire
Bug on checkboxes in laravel livewire

Time:10-26

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 >
     <div >
          <div >
            <x-jet-input wire:model="home" value="{{$home->id}}" type="checkbox"/>
          </div>
          <div >
            <x-jet-label for="home" value="{{$home->garde_type}}"/>
          </div>
      </div>
      <div >
          <div >
            <x-jet-input wire:model='visit' value="{{$visit->id}}" type="checkbox"/>
          </div>
                
          <div >
            <x-jet-label for="visit" value="{{$visit->garde_type}}"/>
          </div>
       </div>               
  </div>

CodePudding user response:

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

$annonces=annonces::create([
    'home' => $this->home,
    'visit' => $this->visit,
]);

CodePudding user response:

Well i tried this at the begining but laravel send me the all rows of the table. I need to do this to send only the ID. But maybe it's another part of the code that is a problem ?

  • Related