简体   繁体   中英

laravel uploaded image not adding to the database

I am trying to make image upload on laravel but at some point it fails to set image destination in database.

When i try and test and echo back final image and link destination everything works fine, when i check the image destination and on my destination folder image is there but only image wont set in database.

I have set this field in database

Name          Type           Collation         Attributes   Null    Default
avatarfull    varchar(500)   utf8_unicode_ci                 No       None

i increased varchar to 500 just in case.

and on my usercontroller i have this for storing new user

    public function store() {
    $validator = Validator::make(Input::all(), [
        'photo'             => 'mimes:jpeg,bmp,png|max:1000'
    ]);

    if($validator->fails()) {
        return Redirect::route('admin.users.create')->withErrors($validator)->withInput();
    } else {
        if(Input::hasFile('photo')) {
            if(Input::file('photo')->isValid()) {
                $imagename = str_random(20) . '.' . Input::file('photo')->getClientOriginalExtension();
                $imagelocation = Input::file('photo')->move(base_path() . '/img', $imagename );
                $avatarfull = 'img/' . $imagename;
            } else {
                $avatarfull     = 'img/profile_default.png';
            }
        } else {
            $avatarfull     = 'img/profile_default.png';
        }

        $create = User::create([
            'avatarfull'    => $avatarfull,
        ]);

        if($create) {
            return Redirect::route('admin.users.index')
                ->with('success_message', 'New user is successfully created!');
        }
    }

}

So the validator checks for what it needs to check, if the image is that mime type, and checks if image size doesn't exceed 1mb, that works.

Further on input checks if hasfile set for upload, if file is set for upload, it checks if file is valid, and than it gets image, rename it to random string, copy to proper location and set $avatarfull variable to proper path for setting it in the database.

If i

echo $avatarfull
or
print_r($avatarfull)
or
var_dump(Input::all())

I get all the data correct as it should be

var_dump shows all the data of input fields and there are laravel's usual backend things like checking the file type, file size, storing it in temp path, moved path to new location. Everything works fine.

if i check at the end $avatarfull variable, just before i store a new user $avatarfull variable is as it should, if image is uploaded than it echoes

img/random10characterstring.jpg

Checking against base path image is properly stored and moved to wanted location on my pc

/opt/lampp/htdocs/laravel/img/random10characterstring.jpg

Using in combination to link_to it shows the final results of url linked image

localhost/laravel/img/random10characterstring.jpg

And of course checking in my file browser the image does exists in that path. So laravel done it's work uploaded image, renamed it, and moved to desired location.

Only it won't set the field in the databas. When i add new user (this is image field for user avatar) it adds new user in the database, and i get set all the fields in the database except i don't get set path to image in database. For some reason this field lefts blank.

Please check if avatarfull is in $fillable array in your User model. If not Users model mass assignment protection will not allow to store the data by Users::create(...) . If you don't want to put avatarfull in fillable array, you can always use:

$user = new User;
$user->avatarfull = $avatarfull;
$user->save();

For more information about mass assignment see: http://laravel.com/docs/4.2/eloquent#mass-assignment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM