0

I have an array and an id of another table. I need each array item to be a new record. Each of these records needs to include one item from the array and the id of the other table. How do I make the store function loop through the array and each time grab the other table id and insert into the database?

The View

<tablv>
     <thead>
        <tr>
           <td>MFG</td>
        </tr>
      </thead>
      <tbody>
         <tr v-for="hp in ordered_handpieces">
             <td>
                <input type="checkbox" :value="hp.id" v-model="hp_id"><span>{{hp.mfg.mfg_name}}</span>
             </td>
         </tr>
      </tbody>
</table>

The Script

        data: function() {
                return {
                    hp_wo_id:'',
                    hp_id:[],
                    }
            },

        methods: {
               onSubmit(){
                 axios.post('/hp_wo_unit', this.$data)   
                
                 }    
            
            }
       

The Controller

public function store(Request $request){
        foreach (request(['hp_id']) as $hp_id) {
            hp_wo_unit::create(request(['hp_wo_id'],$hp_id));
        }
    return $request;
}

1 Answer 1

1

Since you said each on of them to be a new record, here's a simple approach for what you said :

foreach (request(['hp_id']) as $item) {
    hp_wo_unit::create(request(['hp_wo_id'],$item));
    // Or 
    $hp = new MODEL();
    $hp->hp_wo_id = request(['hp_wo_id']);
    $hp->hp_id = $item;
    $hp-save();
    
}
Sign up to request clarification or add additional context in comments.

7 Comments

I added more of the View. Please see above. With your suggestion I am getting one record regardless of array length and the array values are not inserting into the database.
Yes. It seems like it should work but it does not. I am getting one record regardless of array length and the array value is not inserting into the database. I added the view just in case that is where the problem might be. Thanks!
@T.A.return $request and show me the response. idk about your view, since it's looking fine. just show me the response.
Sorry to ask another question but I am not sure where to view the response. How do I see the response in a Vue.js application?
open your page, then use ctrl+i or inspect element, then go in Network tab, and you can see network calls from there. when you hit submit button 1 request call should be added, click on it and go to response tab.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.