[Vuejs]-Updating a Whole Table (Laravel and Vuejs)

0👍

It was a rookie mistake. I was trying to pass a collection of the trades to database, but put request requires /api/trades/$id. That’s why I’ve created a for loop in front-end, and in back-end, all I need was a simple update function.

This is the function that is called when I click the submit button:

publish() {
  var trades = this.form.asset_id.length;
  this.form.post("/api/posts")
  for (var i = 0; i < trades; i++) {
    if (this.form.asset_id[i] in this.form.status) {
        this.form.put("/api/trades/" + this.form.asset_id[i])
    }
  }
}

And this is the upload function:

public function update(Request $request, $id)
{
    $item = Trade::findOrFail($id);
    $input = $request->all();
    $updates = [
        'trade' => $input['trade'][$id],
        'status' => $input['status'][$id],
        'private' => (array_key_exists($id, $input['public_code']) ? 0 : 1)
    ];
    $item->update($updates);
}

Leave a comment