osherdo's avatar

Failing at uploading images with dropzone.js

I am trying to upload an image for a user (as his profile image), and I get this error in the image element in the output. I noticed the error shows after I add this code to the controller. Would love to hear your suggestions about it.

$user = auth->user();

$user->profileImage()->create([
    'filename' => $filename
]);

Error Screenshot:

http://1.1m.yt/Wgsi-uF.png

error - pretty long text, so I have pasted it in this link: https://justpaste.it/xgk8

here's my code: User.php model:

public function profileImage()
  {
    return $this->hasMany(ProfileImage::class);
  }

ProfileImage Model:

class profile_image extends Model
{
    protected $table="profile_images";
    protected $fillable=["image_upload"];


    public function user()
    {
    return $this->belongsTo(User::class);
    }

    public function getPathAttribute()
{
    return $this->filePath;
}

}

Controller:

class DropzoneController extends Controller
{

  public function index()
{
  return view('dropzone_demo');
}

public function uploadFiles()
{
  $user = auth->user();

  $message="Profile Image Created";
  $input = Input::all();

  $rules = array(
      'file' => 'image|max:3000',
  );

  $validation = Validator::make($input, $rules);

  if ($validation->fails()) {
      return Response::make($validation->errors->first(), 400);
  }

  $destinationPath = 'uploads'; // upload path: public/uploads
  $extension = Input::file('file')->getClientOriginalExtension(); // getting file extension
  $fileName = rand(11111, 99999) . '.' . $extension; // renameing image
  $upload_success = Input::file('file')->move($destinationPath, $fileName); // uploading file to given path

  if ($upload_success)
  {
      return Response::json('success', 200);
  } else
  {
      return Response::json('error', 400);
  }

//  attaching the profile image to the authenticated user.

$user->profileImage()->create([
    'filename' => $filename
]);

}
}

view:

    <form action="{{ url('user/upload')}}" class="dropzone" name="image_upload" id="my-awesome-dropzone"></form>

0 likes
1 reply
mstnorris's avatar
Level 55

You haven't supplied the csrf_token!

If you had copied and pasted the error (the one you linked to) into a browser, then you would have spotted that :)

2 likes

Please or to participate in this conversation.