1

I have a code where I want to save images using Vue and Laravel saves the route in the database

The Controller:

public function update(Request $request, $id){
      $home = Home::findOrFail($id);
      $home->background = $request->background;
      $home->title = $request->title;
      $home->subtitle = $request->subtitle;
      $home->icon_go = $request->icon_go;

      $fileName = $request->image;

      $path = $_SERVER['DOCUMENT_ROOT'].'assets/images/'.$fileName;
      $home->image =  $path;

      $home->update();
      file_put_contents($path, $fileName);

      return response()->json([
           'status'=> 200,
            'title' => 'Home Update',
           'data'  => $home,
      ]);
}

The input:

<v-col cols="12" sm="12" md="12">
    <input type="file"
        @change="getImage"
        label="Imagen" 
        required
        :class="{ 'is-invalid' : form.errors.has('image') }">

        <has-error :form="form" field="image"></has-error>
 </v-col>

Only I just put the input, the form is working fine

The function update:

update(){
//Update a resource
this.$Progress.start()
this.form.busy = true;
this.form.image = this.form.image.name
this.form.put('/api/v1/home/' + this.form.id)
        .then(response => {
            this.getHome()
            if (this.form.successful) {
                this.$Progress.finish()
                this.updateNotify()
            }else{
                this.$Progress.fail() 
                this.$snotify.error('¡Ha ocurrido un error!', 'Error')
            }
        })
        .catch(e => {
            this.$Progress.fail() 
            console.log(e)
        })
  }, 

The problem may be in the controller but I cannot detect it. I'd appreciate your help.

The only thing that does not work is that the image is not showing the content

The photo is saved in the folder public / assets / images This is how the image is saved in the folder

1
  • 2
    You are not getting image using $fileName = $request->image; You can get image using $fileName = $request->file('image'); Commented May 25, 2020 at 4:59

1 Answer 1

1

Try using the below code. Since $request->image won't give file object. Instead, we need to use file() helpr.

public function update(Request $request, $id){
      $home = Home::findOrFail($id);
      $home->background = $request->background;
      $home->title = $request->title;
      $home->subtitle = $request->subtitle;
      $home->icon_go = $request->icon_go;

      $file = $request->file('image'); //gets the image file

      $path = $_SERVER['DOCUMENT_ROOT'].'assets/images/';
      $home->image =  $path.$file->getClientOriginalName();

      $home->update();

      $file->move($path, $file->getClientOriginalName()); //stores in location

      return response()->json([
           'status'=> 200,
            'title' => 'Home Update',
           'data'  => $home,
      ]);
}
Sign up to request clarification or add additional context in comments.

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.