mardi 25 août 2020

Image Upload: Error Creating default object from empty value

I'm having trouble with image upload/processing, I only follow a youtube tutorial code by code so I'm not quite sure whats the problem. The image itself doesn't change the name of the supposed file name yet it is moved into the specified image directory with the image original name.

Here is my Controller

public function add(Request $request) {
    $post = new Blog();

    $post->name = $request->input('title');
    $post->content = $request->input('content');
    $post->image = $request->input('image');

    if($request->hasfile('image')) {
        $file = $request->file('image');
        $extension = $file->getClientOriginalExtension();
        $filename = time().'.'. $extension;
        $file->move('uploads/blog/', $filename);
        $blog->image = $filename;
    }else{
        return $request;
        $blog->image = '';
    }

    $post->save();

    return view('admin.postnews')->with('post', $data);
}

}

and here is my Form

                    <form action="" method="POST" enctype="multipart/form-data">
                
                 ... 
                  <div class="form-group">
                    <label for="exampleFormControlFile1">Thumbnail</label>
                    <input type="file" name="image" class="form-control-file" id="exampleFormControlFile1">
                  </div>
                <button type="submit" class="btn btn-primary">Submit</button>
              </form>

I had google similar questions to no avail since everyone seems to have a different way of image processing or their problem is not with image.

For a record im currently using Laravel 5.8



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3aYM9Eb
via IFTTT

Aucun commentaire:

Enregistrer un commentaire