jeudi 7 juin 2018

Model::updateOrCreate actually updates but does not create when record does not exist

I am building an app where if a user wants to update any records the user can edit and update but also add more fields on the front-end so that does data entered in the fields can be added to the records and if user edited anything, that too can updated change successfully.

So there are tests(question) that a user is supposed to do and each test has options just like an examination.

When I try to change anything and click on the update button the record gets updated successfully but if I add a new input as part of the form, it only updates the record and does not add the new one.

This is my HTML:

<table class="table table-bordered">
  <thead>
    <tr>
        <th>#</th>
        <th>Contraceptive pill</th>
        <th>UID</th>
        <th>Implant</th>
        <th>Injectable</th>
        <th>Male condom</th>
        <th>Female condom</th>
        <th>Fertility awareness</th>
        <th>Withdrawal</th>
        <th>Sterilization</th>
        <th>Emergency contraceptive</th>
        <th>Actions</th>
    </tr>
 </thead>
 <tbody id="test-inputs">
    @foreach($Test0ptions as $option)      
    <tr>

    <td></td>

    <td>
        <input class="form-control" type="number" name="contraceptive_pills[]" required value=""> 

   </td>

    <td> 
        <input class="form-control" type="number" name="uids[]" required value=""> 
    </td>

    <td> 
        <input class="form-control" type="number" name="implants[]" required value=""> 
    </td>

    <td> 
        <input class="form-control" type="number" name="injectables[]"  required value=""> 
    </td >

    <td> 
        <input class="form-control" type="number" name="male_condoms[]"  required value=""> 
    </td>

    <td> 
          <input class="form-control" type="number" name="female_condoms[]" required value=""> 
    </td>

    <td> 
        <input class="form-control" type="number" name="fertility_awareness[]" required value=""> 
    </td>

    <td> 
       <input class="form-control" type="number" name="withdrawals[]" required value=""> 
    </td>

    <td> 
        <input class="form-control" type="number" name="sterilizations[]" required value=""> 
    </td>

    <td> 
        <input class="form-control" type="number" name="emergency_contraceptives[]" required value=""> 
    </td>

   </tr>

  </tbody>
 </table>

<div class="form-group" style="padding-bottom: 10px;">
    <button type="submit" class="btn primary"><i class="fa fa-save"></i> Update</button>
</div>

This is my script that will add new set of form:

 populateNewTestForms() {
        $('#test-inputs').append(`
            <tr>

        <td>${this.incrementTableCounter+=1}</td>

        <td><input class="form-control" type="number" name="contraceptive_pills[]" required> </td>

        <td> <input class="form-control" type="number" name="uids[]" required> </td>

        <td> <input class="form-control" type="number" name="implants[]" required> </td>

        <td> <input class="form-control" type="number" name="injectables[]" required> </td>

        <td> <input class="form-control" type="number" name="male_condoms[]" required> </td>

        <td> <input class="form-control" type="number" name="female_condoms[]" required> </td>

        <td> <input class="form-control" type="number" name="fertility_awareness[]" required> </td>

        <td> <input class="form-control" type="number" name="withdrawals[]" required> </td>

        <td> <input class="form-control" type="number" name="sterilizations[]" required> </td>

        <td> <input class="form-control" type="number" name="emergency_contraceptives[]" required> 
        </td>

        <td>

<button type="button" onclick=" $(this).closest('tr').remove(); " class="btn btn-danger" title="delete"><i class="fa fa-trash"></i></button>

        </td>   

    </tr>
        `);

This is my controller :

  foreach ($request->option_ids as $option_id) {

        TestOption::updateOrCreate(['id'   =>  $option_id], [

            'test_id'   =>  $test->id,

            'name'      =>  $request->names[$i],

            'contraceptive_pill'      =>  $request->contraceptive_pills[$i],

            'uid'     =>  $request->uids[$i],

            'implant'     =>  $request->implants[$i],

            'injectable'     =>  $request->injectables[$i],

            'male_condom'     =>  $request->male_condoms[$i],      

            'female_condom'     =>  $request->female_condoms[$i],

            'fertility_awareness'     =>  $request->fertility_awareness[$i],

            'withdrawal'     =>  $request->withdrawals[$i],

            'sterilization'     =>  $request->sterilizations[$i],

            'emergency_contraceptive'     =>  $request->emergency_contraceptives[$i]

            ]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire