在Laravel注册中上传个人资料图片失败

时间:2018-09-05 13:25:38

标签: laravel image registration profile

我想在注册时上传个人资料图片

在我必须注册用户之前,之后可以将默认的user.jpg更改为我自己的个人资料图片

但是现在我想在注册时上传我的个人资料照片

这是我要上传图片的视图部分:

<div class="form-group col-md-12">
                <label>Firmenlogo</label>
                <div class="custom-file">
                    <input type="file" name="avatar" class="custom-file-input" id="validatedCustomFile" required>
                    <input type="hidden" name="_token" value="{{csrf_token()}}">
                    <label class="custom-file-label" for="validatedCustomFile">Datei auswählen...</label>
                    <div class="invalid-feedback">Bitte laden Sie hier Ihr Firmenlogo hoch.</div>
                </div>
            </div>

这是我的控制器:

<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{

use RegistersUsers;

protected $redirectTo = '/admin';

public function __construct()
{
    $this->middleware('guest');
}

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'avatar' => 'required|string',
    ]);
}

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'avatar' => $avatar,
    ]);
}

}

这是我的迁移文件:

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('avatar')->default('default.jpg');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

这是我的user.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $fillable = [
        'name', 'email', 'password', 'avatar',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

所以Everyythign可以将pciture作为字符串保存在我的数据库中,例如picture.jpg,但不会将其保存在我的上载文件夹中,所以我尝试这样做:

$avatar = $data['avatar']->file('avatar');
    $filename = time() . '.' . $avatar->getClientOriginalExtension();
    Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/avatars/'.$filename));

但是什么也没发生 它不会在我的数据库中保存任何内容 如果我单击注册页面重新加载,但没有任何反应,我不知道为什么 而且我不知道该怎么办 我尝试了很多事情,读了很多博客,但没有用

只有在注册后才能进入个人资料并上传新照片

但是为什么?所以我知道代码有效,但为什么不能在寄存器控制器中使用?

4 个答案:

答案 0 :(得分:1)

首先检查您的表单是否具有 enctype =“ multipart / form-data” ,并检查您的 App \ User.php $ fillable strong>数组在数组中包含“头像” 键,用于将图像插入数据库。

答案 1 :(得分:1)

我认为您正在寻找这个东西:

protected function create(Request $request)
{
    if($request->hasFile('avatar')) {

        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/avatars/'.$filename));
        $avatarPath = '/uploads/avatars/'.$filename;

        $user = new User;
        $user->name = $request['name'];
        $user->email = $request['email'];
        $user->password = Hash::make($request['password']);
        $user->avatar = $avatarPath;
        $user->save();

        return redirect()->back(); /** or somewhere else **/
    }

}

答案 2 :(得分:1)

我知道了!这样愚蠢的东西!

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'avatar' => 'required',
     ]);
    }

在此功能中,我需要头像和STRING,删除后一切正常!!! OMG现在很开心

这是我现在的创建函数:

 protected function create(array $data)
{
    $request = app('request');
    if($request->hasfile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(300, 300)->save( public_path('/uploads/avatars/' . $filename) );
    }
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'avatar' => $filename,
    ]);
}

答案 3 :(得分:0)

您不应在$ fillable变量中保留“头像”列。您应该删除它。