skip to Main Content

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


  1. Chosen as BEST ANSWER

    Well now my checkboxes don't bug any more. But it won't work as i want to.

    This is my checkboxes :

       <x-jet-input wire:model="home"  value="1" type="checkbox"/>
                                    
       <x-jet-input wire:model="visit" value="2" type="checkbox"/>
                                    
                                    
                                   
    

    Here my controler :

    public $visit;
    public $home;
    
    
    
    public function submit()
    {
     
    
       $annonces=annonces::create([
            
            'home' => $this->home,
            'visit' => $this->visit,
          
        ]);
       
       
       
        $annonces->save();
    
       }
    

    But i don't want to pass the value "1" and "2" in the form.

    What i tried is that :

    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();
    
       }
    

    And in the form :

      <x-jet-input wire:model="home"  value="{{home->id}}" type="checkbox"/>
                                    
       <x-jet-input wire:model="visit" value="{{visit->id}}" type="checkbox"/>
    

    But all i tried doesn't worked.

    Any idea?


  2. 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,
    ]);
    
    Login or Signup to reply.
  3. 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.

    // 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"/>
    

    Why are you trying to get the ID for the Garde_type here, when you already have it?

    // 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;
    

    What are you trying to accomplish exactly?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search