skip to Main Content

This is the code for storing an array only. What I want now is to update each field or can add new rows but I don’t know how.

        foreach ($request->requirements as $key => $requirements) {
        $req = new FormReq();
        $req->requirements = $requirements['title'];
        $req->form_id = $id;
        $req->save();
    }

My blade file

@foreach ($requirements as $reqs)
    <tr>
        <td><input type="text" name="requirements[0][title]"
            placeholder="Enter requirements" class="form-control"
            value="{{ $reqs->requirements }}" />
        </td>
        <td><button type="button" class="btn btn-sm btn-danger remove-tr">Remove</button>
        </td>
    </tr>
@endforeach

enter image description here
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Credit to Yannick's answer I was able to handle the create and update in Edit page, but I added some codes in blade in order for this to work.

    I added this code to the script as well

    <input type="hidden" name="requirements['+ i +'][id]" value="" />
    

    var i = 0;
    $("#add-btn").click(function() {
      ++i;
      $("#dynamicAddRemove").append('<tr><td><input type="text" name="requirements[' + i +
        '][title]" placeholder="Enter requirement" class="form-control" />  <input type="hidden" name="requirements[' +
        i +
        '][id]" value="" /> </td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>'
      );
    });
    $(document).on('click', '.remove-tr', function() {
      $(this).parents('tr').remove();
    });
    //handle deletion for existing field
    $('.deleteReq').click(function(e) {
      e.preventDefault();
    
      var req_id = $(this).val();
      $('#req_id').val(req_id)
      $('#deleteModal').modal('show');
    
    });

    I added key to create unique fields

    @foreach ($requirements as $key => $reqs)
    <tr>
      <td><input type="text" name="requirements[{{ $key }}][title]" placeholder="Enter requirement" class="form-control" value="{{ $reqs->requirements }}" />
        <input type="hidden" name="requirements[{{ $key }}][id]" value="{{ $reqs->id }}" />
      </td>
      <td><button type="button" class="btn btn-danger remove-tr deleteReq" value="{{ $reqs->id }}">Remove</button>
      </td>
    </tr>
    @endforeach


  2. First add an input hidden in your existing items. Adapt the name of the input if this doesn’t work

    <input type="hidden" name="requirements[][id]" value="{{ $reqs->id }}" />
    

    Then check for the presence of the id in your method

    foreach ($request->requirements as $key => $requirements) {
    
        // Update
        if (isset($requirements['id']) && $requirements['id']) {
            $req = FormReq::find($requirements['id']);
            $req->requirements = $requirements['title'];
        // Create
        } else {
            $req = new FormReq();
            $req->requirements = $requirements['title'];
            $req->form_id = $id;
        }
        $req->save();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search