Leff7's avatar
Level 4

Laravel 5.3 - image upload not working

I have an input field for image upload in my view:

              <input type="file" class="form-control" name="image">

I have created folder uploads in my public folder and in my Controller I am trying to upload a file like this:

$path = $request->file('image')->store('uploads/', 'public');

When I submit the form and upload an image nothing gets uploaded to the folder uploads and I get the error:

SQLSTATE[HY000]: General error: 1364 Field 'image' doesn't have a default value
0 likes
11 replies
Zakaria-Acharki's avatar

Try :

$imageName = $request->file('image')->getClientOriginalName();

$request->file('image')->move(
    base_path() . '/public/uploads/', $imageName
);

Hope this helps.

1 like
Indemnity83's avatar

You have a column in your table named image that is not nullable and you're never passing any data to it. Assuming you wanted the path to the image you need to add $model->update(['image' => $path]); line to your script above (where $model is whatever instance variable you use)

1 like
Leff7's avatar
Level 4

@Indemnity83 But the image never gets uploaded in the first place, I was wondering mostly about that, I am aware of the error, I will set the field in the DB to nullable, but I wonder why nothing gets uploaded to the server?

Indemnity83's avatar

Sorry. Typing on phone and got carried away. The update won't work now that I think about it.

You must have a Model::create call in this code somewhere; you need to add the 'image' => $path in there; not in an update.

What does the rest of your controller method look like.

Indemnity83's avatar
public function store(Request $request)
{
    // Do your validation

    $path = $request->file('image')->store('uploads/', 'public');

    $thing = Thing::create([
        'field1' => $request->input('field1'),
        'field2' => $request->input('field2'),
        'field3' => $request->input('field3'),
        'image' => $path,
    ]);
    
}
Leff7's avatar
Level 4

@Indemnity83 Sorry, for the late reply, but I also have the setup like you have shown, in my controller, only the image is not being uploaded.

Indemnity83's avatar

The error your listing points to a problem in your Thing::create() call though; not the image upload. What does your controller method look like? It can't be identical to what I listed as an example.

Leff7's avatar
Level 4

I got rid of the error, not sure why did it even show in the first place, by uploading again, but still nothing gets uploaded, I get a value for $path, that looks like this:

uploads/d87478f7c4717c356353a760fab9addc.jpeg

This is my controller:

public function store(Request $request)
    { 
        $path = $request->file('image')->store('uploads', 'public');

        $club = Club::create([
          'image' => $path,
          'name' => $request->input('name'),
          'adress' => $request->input('adress'),
          'country' => $request->input('country'),
          'city' => $request->input('city'),
          'telephone' => $request->input('telephone'),
          'email' => $request->input('email'),
          'manager' => $request->input('manager'),
          'established' => $request->input('established'),
        ]);

        return view('clubs.show', compact('club'));
    }

I can use the old way of uploading an image and then it works:

        $file = $request->file('image');
        $original_name = $file->getClientOriginalName();
        $imageName = uniqid(true) . $original_name;
        $path = public_path('uploads/' . $imageName);
        Image::make($file)->save($path);

But I wonder why this in 5.3 version recommended way won't work?

Indemnity83's avatar

so you're saying that the store method is returning a value of uploads/d87478f7c4717c356353a760fab9addc.jpeg but there is no file there?

have you looked in /storage/app/ and /storage/app/public?

Leff7's avatar
Level 4

Yes, of course, it is saving them in the storage file. Sorry, for that, haven't checked in the config where the local disk is leading to. Thank you for all your help!

michaeldunga's avatar

This is for others who need help with this, just go to php.ini in xampp and change the following to this:

upload file size max:

  • upload_max_filesize = 50M
  • post_max_size = 50M
  • max_input_time = 300
  • max_execution_time = 300

this worked for me.

Please or to participate in this conversation.