0

I'm trying to update some data in Model using API in Laravel and Vue.js but I can't do this because FormData() is empty, I'm checking the data from formdata using the code:

for (var value of formdata.values()) {
        console.log(value);
      }

and it was empty.

I get this result:

enter image description here

this is my update function in vue page

    getFile: function (event) {
      this.image = event.target.files[0];
    },
    updateData: function () {
      let config = {
        header: {
          "Content-Type": "multipart/form-data",
        },
      };
      let formdata = new FormData();
      formdata.append("user_id", this.userId);
      formdata.append("image", this.image);
      formdata.append("name", this.name);
      formdata.append("price", this.price);
      formdata.append("details", this.details);
      formdata.append("subject", this.subject);
      formdata.append("_method", "PUT");
      for (var value of formdata.values()) {
        console.log(value);
      }

      axios
        .post(
          "http://localhost:8000/api/edit-pro/" + this.products.id,
          formdata,
          config
        )
        .then(() => {
          //this.$router.push("/control-panel", response);
        });
    },

controller method where I should update data

public function updatePro(Request $request, $id)
    {
        $product = Product::find($id);

        $product->name = $request->name;
        $product->price = $request->price;
        $product->details = $request->details;
        $product->subject = $request->subject;
        
        $file = $request->file('image');
        $fileName = $file->image;
        $destinationPath = public_path() . '/images';
        $file->move($destinationPath, $fileName);
        $product->image =  $fileName;
        
        try {
            $product->save();
            return ["msg" => "The product was updated successfully"];
        } catch (Exception $error) {
            return [
                "msg" => "The product was not updated successfully",
                "error" => $error,
            ];
        }
    }

HTML with vue.js where I use object which I send to function

<form
              id="main-contact-form"
              class="contact-form row"
              name="contact-form"
            >
              <div class="form-group col-md-6">
                <input
                  type="text"
                  name="name"
                  class="form-control"
                  required="required"
                  placeholder="اسم المنتج"
                  v-model="products.name"
                />
              </div>
              <div class="form-group col-md-6">
                <input
                  type="text"
                  name="price"
                  class="form-control"
                  required="required"
                  placeholder="السعر"
                  v-model="products.price"
                />
              </div>
              <div class="form-group col-md-6">
                <select
                  name="subject"
                  class="form-control"
                  v-model="products.subject"
                >
                  <option value="اكسسوريز">اكسسوريز</option>
                  <option value="عنايه">عنايه</option>
                  <option value="مكياج">مكياج</option>
                  <option value="شنط">شنط</option>
                  <option value="عطور">عطور</option>
                  <option value="اجهزه">اجهزه</option>
                  <option value="ملابس نساء">ملابس نساء</option>
                  <option value="رجال">رجال</option>
                </select>
              </div>
              <div class="form-group col-md-6">
                <input
                  ref="image"
                  @change="getFile($event)"
                  type="file"
                  name="image"
                  class="form-control"
                  placeholder="اختر صورة المنتج"
                />
              </div>
              <div class="form-group col-md-12">
                <textarea
                  name="message"
                  id="message"
                  required="required"
                  class="form-control"
                  rows="8"
                  placeholder="وصف المنتج أو نبذة عنه"
                  v-model="products.details"
                ></textarea>
              </div>
              <div class="form-group col-md-12">
                <input
                  type="button"
                  name="submit"
                  class="btn btn-primary pull-right"
                  value="تعديل"
                  @click="updateData(products.id)"
                />
              </div>
            </form>

Let me know if you have any additional questions

Thank you guys a lot for any help and ideas!

1 Answer 1

1

Its look like you have a property products and that property have the values you are trying to get.

so instead doing:

formdata.append("image", this.image);
formdata.append("name", this.name);
formdata.append("price", this.price);
formdata.append("details", this.details);
formdata.append("subject", this.subject);

do:

formdata.append("image", this.image);
formdata.append("name", this.products.name);
formdata.append("price", this.products.price);
formdata.append("details", this.products.details);
formdata.append("subject", this.products.subject);
Sign up to request clarification or add additional context in comments.

4 Comments

ooh thanks it's working, But there is still an error in the image: message: "Attempt to read property "image" on null"
this is if i edit: this.products.image. But if i leave it (this.image) i get this message: "Undefined property: Illuminate\Http\UploadedFile::$image"
this.image its the right way
to get the name of the file in larval use ->getClientOriginalName()

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.