laravel-5laravel-livewirelaravel-10livewire-3

livewire 3 how to store foreach value using form


hello I'm new for livewire 3 I have table inside form, data table having radio button for yes or no after selecting yes or no I'm going to submit that form that time I want to store fetch detail like id and name along this. now I'm any getting radio button value, me to solve

form page enter image description here my code form

<div class="modal-body">
    <form wire:submit.prevent="Save" autocomplete="off">
      <div class="card card-bordered card-preview table-responsive ">
        <div class="card-inner "> 
          <table class="datatable  table   ">
            <thead>
              <tr>
                <th style=" border: 1px solid black; text-align: center;">SL</th>
                <th style=" border: 1px solid black; text-align: center;">ID No</th> 
                <th style=" border: 1px solid black; text-align: center;">NAME</th>
                <th style=" border: 1px solid black; text-align: center;">YES</th>
                <th style=" border: 1px solid black; text-align: center;">NO</th>
              </tr>
            </thead>
            <tbody> 
 @foreach ($UserDetail as $key=>$UserDetails) 
 @php $ID = 0+$key @endphp
<td style=" border: 1px solid black; text-align: center;">
                {{ $key + 1  }}
              </td> 
              <td style=" border: 1px solid black; text-align: center;">
                {{ $UserDetails->id }}
              </td>
              <td style=" border: 1px solid black; text-align: center;">
                {{ $UserDetails->name }}
              </td>
              <td style=" border: 1px solid black; text-align: center;">
                <div class="custom-control custom-control-md custom-radio ">
                  <input type="radio" wire:model="TableInput.{{$ID}}.data" class="custom-control-input" name="TableInputs[{{$ID}}]" id="sv2-preference-fedev{{$ID}}" value="YES" required>
                  <label class="custom-control-label" for="sv2-preference-fedev{{$ID}}"></label>
                </div>
              </td>
              <td style=" border: 1px solid black; text-align: center;">
                <div style="text-align: center;" class="custom-control custom-control-md custom radio"><input type="radio" wire:model="TableInput.{{$ID}}.data" class="custom-control-input" name="TableInputs[{{$ID}}]" id="sv2-preference-uxdis{{$ID}}" value="NO" required>
                  <label class="custom-control-label" for="sv2-preference-uxdis{{$ID}}"></label>
                </div>
              </td> 
              </tr> 
      @endforeach 
            </tbody>
          </table>
        </div>
      </div>
      <br>
      <div  >
        <button type="submit" class="btn btn-md btn-primary">SAVE  </button>
      </div>
    </form>
  </div>

Controller class StudentAttendance extends Component {

public $name; 
public $id;  
public $TableInput = [];


public function mount()
{
  $this->User       = User::all();   
}

public function Save() 
{   
    $bel = Data::create([ 
         
        'Id'                     => $value['Id'],
        'name'                   => $value['name'],
        'data'                   => $value['data'],
    ]);
} 
} 

}


Solution

  • In order to save the form on the click of save button, you may create the table in the form with the help of an array. You can convert your collection to array like this:

    $this->user = User::get()->toArray();
    

    then in your blade file you can be bind the model with input fields as:

    @foreach ($user as $key => $value)
        <tr>
            <td>...</td>
            <td style=" border: 1px solid black; text-align: center;">
                <input type="text" wire:model="user.{{ $key }}.first_name">
            </td>
            <td>...</td>
            <td>...</td>
            <td>...</td>
        </tr>
    @endforeach
    

    In this manner, you can iterate through the user array within the save() method to save the data:

    public function save()
    {
        ...
        ...
        foreach ($this->user  as $key => $value) {
            $bel = Data::create($value);
        }
        ...
    
    }
    

    You can also use batch insert/update as per your usecase.