简体   繁体   English

laravel上传的图像未添加到数据库

[英]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. 我正在尝试在laravel上上传图像,但有时无法在数据库中设置图像目标。

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. 我将varchar增加到500,以防万一。

and on my usercontroller i have this for storing new user 在我的usercontroller上,我有这个用于存储新用户

    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. 因此,验证器会检查需要检查的内容(如果图像是该mime类型),并检查图像大小是否不超过1mb,就可以了。

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. 进一步在输入时检查是否已将hasfile设置为要上传,是否已将文件设置为要上传,它将检查文件是否有效,然后获取图像,将其重命名为随机字符串,复制到正确的位置,并将$ avatarfull变量设置为正确的设置路径它在数据库中。

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. var_dump显示输入字段的所有数据,还有laravel通常的后端操作,例如检查文件类型,文件大小,将其存储在临时路径中,将路径移动到新位置。 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 如果我在末尾检查$ avatarfull变量,则在我存储新用户$ avatarfull变量之前,它应该是正确的,如果上传的图像比它回显的多

img/random10characterstring.jpg

Checking against base path image is properly stored and moved to wanted location on my pc 检查基本路径图像是否正确存储并移至我的PC上的所需位置

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

Using in combination to link_to it shows the final results of url linked image 结合使用link_to可以显示url链接图像的最终结果

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. 因此laravel完成了上传图片的工作,将其重命名并移动到了所需的位置。

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. 请检查您的用户模型中avatarfull是否在$ fillable数组中。 If not Users model mass assignment protection will not allow to store the data by Users::create(...) . 如果不是,则用户模型批量分配保护将不允许通过Users::create(...)存储数据。 If you don't want to put avatarfull in fillable array, you can always use: 如果您不想将avatarfull放入可填充数组中,则可以始终使用:

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

For more information about mass assignment see: http://laravel.com/docs/4.2/eloquent#mass-assignment 有关批量分配的更多信息,请参见: http : //laravel.com/docs/4.2/eloquent#mass-assignment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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