Laravel 5在注册时插入图像 [英] Laravel 5 inserting image when register

查看:67
本文介绍了Laravel 5在注册时插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解的这段代码:

   return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'image'    => $data->file('image')
                        ->move(
                           public_path('images'), 
                           $request->file('image')
                                   ->getClientOriginalName())

创建一个用户.问题是,当我以注册形式选择图像文件时,无法将其上传到数据库的配置文件表中.我不知道如何传递代码的最后一行以使其正常工作:

creates a user. The problem is when I select the image file in registration form I can't upload it to the database's profile table. I don't know how to pass that last line of the code to make it work:

 'image' => $data->file('image')
                    ->move(
                       public_path('images'), 
                       $request->file('image')
                               ->getClientOriginalName())

推荐答案

如果要将文件存储在服务器上,并将文件路径存储在数据库中,则代码应如下所示.

If you want to store the file on the server and the file path in the database your code could look like this.

 $user = new User();
 $user->name = Input::get('name');
 $user->email = Input::get('email');
 $user->password = hash(Input::get('password'));
 if(Input::hasFile('image')){
    $file = Input::file('image');
    $file = $file->move(public_path().'/images/',$file->getOriginalFileName());
    $user->image = $file->getRealPath();
 }
 $user->save();
 return $user;

如果要将图像存储在数据库中,则必须对此稍作调整.

If you want to store the image in the database you would have to adjust this slightly.

这篇关于Laravel 5在注册时插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆