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

Posted 1 year ago by nanadjei2

I am building an app where if a user wants to update records he/she can edit and add more fields so that does data in the fields can be added to the already existing records and he/she edited anything that too can change successfully. I hope you get the basic idea.

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">
                
        <tr v-for="(option, index) in options">

        <td>@{{ index + 1}}</td>

        <td>
            <input class="form-control" :class="`optionId${option.id}`" 
                   type="number" name="contraceptive_pills[]" :value="option.contraceptive_pill" required> 

            </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="uids[]" :value="option.uid" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="implants[]" :value="option.implant" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="injectables[]" :value="option.injectable" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="male_condoms[]" :value="option.male_condom" required> 
        </td>

        <td> <input class="form-control"  :class="`optionId${option.id}`" 
                type="number" name="female_condoms[]" :value="option.female_condom" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="fertility_awareness[]" :value="option.fertility_awareness" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="withdrawals[]" :value="option.withdrawal" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="sterilizations[]" :value="option.sterilization" required> 
        </td>

        <td> <input class="form-control" :class="`optionId${option.id}`" 
                type="number" name="emergency_contraceptives[]" :value="option.emergency_contraceptive" required> 
        </td>
                            
        <td>
                        
        <a :id="`deleteBtn${option.id}`" @click.prevent="deleteRow(index, option.id)" 
                        :href="`${baseUrl}/delete/${option.id}`" 
                            type="submit" class="btn btn-danger" title="delete"><i class="fa fa-trash"></i>
                  </a>

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

                ]);

It is able to update but not create. Thank you.

Please sign in or create an account to participate in this conversation.