0

I created a component that can add additional fields by pressing a button. I don't know how would I submit this in the database with axios.post and laravel controller. I was able to achieve it in the past with the use of jquery and pure laravel, but I'm confused how to implement it in vue and axios.

Component.vue

<template>
  <v-app>
  <table class="table">
    <thead>
      <tr>
        <td><strong>Title</strong></td>
        <td><strong>Description</strong></td>
        <td><strong>File</strong></td>
        <td></td>
      </tr>
    </thead>  
    <tbody>
      <tr v-for="(row, index) in rows" :key="row.id">
        <td><v-text-field outlined v-model="row.title" /></td>
        <td><v-text-field outlined v-model="row.description" /></td>
        <td>
          <label class="fileContainer">
          <input type="file" @change="setFilename($event, row)" :id="index">
          </label>
        </td>
        <td><a @click="removeElement(index);" style="cursor: pointer">X</a></td>
      </tr>
    </tbody>
  </table>
    <div>
      <v-btn @click="addRow()">Add row</v-btn>
      <v-btn class="success" @click="save()">Save</v-btn>
      <pre>{{ rows | json}}</pre>
    </div>
  </v-app>
</template>

<script>
  export default {
    data: ()=> ({
      rows: []
    }),
    methods: {
      addRow() {
        var elem = document.createElement('tr');
        this.rows.push({
          title: "",
          description: "",
          file: {
            name: 'Choose File'
          }
        });
      },
      removeElement(index) {
          this.rows.splice(index, 1);
      },
      setFilename(event, row) {
        var file = event.target.files[0];
        row.file = file
      },
      save() {
        // axios.post
      }
    }
  }
</script>

Controller.php

 public function store(Request $request) 
{
  // store function
}

3 Answers 3

1
 save() {
 let data = this.rows
          axios
            .post("Url", {
                data
            })
            .then((res) => {
               console.log(res);
            })
            .catch((err) => {
                console.log(err)
            });
      }

ref link https://github.com/axios/axios

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for this. I am now receiving a post 500 internal error which I believe is an error in the controller. After searching, I tried this code and it seems that it is incorrect. public function store(Request $request) { $products = json_decode($request->data, true); foreach( $products as $product ) { EmployeeObjective::create([ 'kpi_info' => $product['title'], 'kpa_info' => $product['description'], ]); } }
i have give you axios solution not your controller solution you can dd() what request you are getting then u can write code accordingly
0
save() {
       axios
            .post("/your/uri", {
                user_id: 1,
                user_name:'jhone doe',
                email:'[email protected]' 
            })
            .then(response => {
               console.log(response);
            })
            .catch(error => {
                console.log(error)
            });
      }

You can retrieve your data from your controller $request->user_id,...,$request->email

Tip: if you post any object,you must JSON.stringify(your_json) them and in a response data from controller json_decode($your_json,true) or you need to modify your header file.

Always use '/your/uri' instead of /your/uri/ without trailing '/'

1 Comment

please give up-vote and if you think this is your answer,please mark it as a answer- it will be appreciated.
0

It now works. I'll be posting my code just in case someone encounter the same hurdle. Than you very much to @kamlesh-paul and @md-amirozzaman

Component.vue

<script>
  export default {
    data: ()=> ({
      rows: [],
    }),

    methods: {
      addRow() {
        this.rows.push({
          corporate_objective_id: '',
          kpa: '',
          kpi: '',
          weight: '',
          score: '',
          equal: '',
          file: {
            name: 'Choose File'
          }
        });
      },
      removeElement(index) {
          this.rows.splice(index, 1);
      },
      setFilename(event, row) {
        var file = event.target.files[0];
        row.file = file
      },
      save() {
        const postData = {
          data: this.rows
        }
        console.log(postData)
        axios
        .post('/api/employee-objective', {postData})
        .then(res => { console.log(res) })
        .catch(err => { console.log(err) });
      }
    }
  }
</script>

Controller.php

  public function store(Request $request) {
    foreach($request->data as $data) {
      $container = EmployeeObjective::updateOrCreate([
        'kpa_info' => $data['kpa'],
        'kpi_info' => $data['kpi'],
        'kpa_weight' => $data['weight'],
        'kpa_score_1' => $data['score'],
        'kpa_equal' => $data['equal'],
       ]);
      $container->save();
    }
  }

Comments

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.