how to upload image in nuxt.js using laravel backend API

Viewed 1479

nuxt.js i searched lots of tutorial for image uploading using laravel api but i not get logic how to code image uploading things help me to solve this or give tutorial links .

how to make image uploading form in nuxt.js

i Created laravel API for image uploading.

my Router

Route::group(['middleware' => 'auth:api'], function() {
Route::post('/Employeeregister', 'EMPLOYEE_API\RegisterController@register')->name('Employeeregister');

}); 

CONTROLLER CODE

 public function imageUploadPost(Request $request)
    {
        $request->validate([
            'name' =>  'required | string',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
  
        $imageName = time().'.'.$request->image->extension();  
   
        $request->image->move(public_path('images'), $imageName);
   
        return back()
            ->with('success','You have successfully upload image.')
            ->with('image',$imageName);
   
    }

MY Nuxt code

<template>
    <v-row justify="center">
        <v-col cols="12" sm="6">
            <form @submit.prevent="submit">
                <v-card ref="form" >
                    <v-card-text>
                        <h3 class="text-center">Register</h3>
                        <v-divider class="mt-3"></v-divider>
                        <v-col cols="12" sm="12">
                            <v-text-field v-model.trim="form.name" type="text" label="Full Name" solo autocomplete="off"></v-text-field>
                        </v-col>
<v-col cols="12" sm="12"><v-file-field v-model.trim="form.image" type="file" label="image" solo autocomplete="off"></v-file-field>
                            </v-col>
                    </v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <div class="text-center">
                            <v-btn rounded type="submit" color="primary" dark>Register</v-btn>
                        </div>
                    </v-card-actions>
                </v-card>
            </form>
        </v-col>
    </v-row>
</template>
< script >
  export default {
    middleware: ['guest'],
    data() {
      return {
        form: {
          name: '',image: '',
        }
      }
    },
  } <
  /script>
1 Answers

There's a few things to consider.

First, your template form.

<form @submit.prevent="submit">
  <v-card ref="form" >
    <v-card-text>
      <h3 class="text-center">Register</h3>
      <v-divider class="mt-3"></v-divider>
      <v-col cols="12" sm="12">
        <v-text-field v-model.trim="form.name" type="text" label="Full Name" solo autocomplete="off"></v-text-field>
      </v-col>
      <v-col cols="12" sm="12">
        <v-file-field v-model.trim="form.image" type="file" label="image" solo autocomplete="off"></v-file-field>
      </v-col>
    </v-card-text>
    <v-card-actions>
      <v-spacer></v-spacer>
      <div class="text-center">
        <v-btn rounded type="submit" color="primary" dark>Register</v-btn>
      </div>
    </v-card-actions>
  </v-card>
</form>

Your input fields are bound to form.name and form.image, which is correct. In order to submit the form, you have tied the submit button to a method named submit (@submit.prevent="submit"), but you haven't created that method. So nothing happens! (You'll see a warning in your console.)

To create the method, you add it to your component under the methods: property.

export default {
  middleware: ['guest'],
  data() {
    return {
      form: {
        name: '',image: '',
      }
    }
  },
  methods: {
    async submit() {
      let rsp = await this.$axios.$post('/Employeeregister', this.form, {
        'content-type': 'multipart/form-data'
      })
      console.log(rsp.response)
    }
  }
}

Sending the form data to your Laravel API is straight forward, but there are a few considerations. Where is your nuxt app and laravel API hosted? eg. http://localhost:3000, http://localhost:80 etc. I'll update this answer based on your response.

Related