1

我正在尝试在注册期间上传用户照片,但出现此错误,我在网上搜索并发现可能的解决方案对我不起作用。我正在使用哨兵包进行高级身份验证。请任何人帮助我。这是我的控制器-

 public function postRegister(Request $request){
    $request->validate([
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'user_name' => 'unique:users|max:255',
        'email' => 'required|unique:users|email|max:255',
        'password' => 'required|min:6|confirmed',
        'phone' => 'required|numeric',
    ]);
    //upload image
    if ($file = $request->file('photo')) {
        $fileName = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension() ?: 'png';
        $folderName = '/uploads/users/';
        $destinationPath = public_path() . $folderName;
        $safeName = str_random(10) . '.' . $extension;
        $file->move($destinationPath, $safeName);
        $request['photo'] = $safeName;
    }

    // $user = Sentinel::registerAndActivate($request->all());
    $user = Sentinel::registerAndActivate($request->all());
    // dd($user);
    //$activation = Activation::create($user);

    $role = Sentinel::findRoleBySlug('member');

    $role->users()->attach($user);
    //$this->sendEmail($user, $activation->code);
    //return redirect('/login');
    return ('You have seccsessfully registered. Now login to start');
}

和表单输入-(带有 enctype="multipart/form-data" 表单顶部)

 <div class="row">
    <div class="col-md-6">
      <div class="sponsor-row">
        <label for="sponsor_input"><i class="fa fa-tags"></i></label>
        <input type="text" name="sponsor" id="sponsor_input" class="sponsor-input" placeholder="Sponsor"></input>
      </div>
    </div>
    <div class="col-md-6">
      <div class="photo-row">
        <label for="photo_input"></label>
        <input type="file" name="photo" id="photo" class="photo-input"></input>
      </div>
    </div>
  </div>
</div>

我的用户模型是:

<?php

命名空间应用程序;

使用 Illuminate\Notifications\Notifiable;使用 Illuminate\Foundation\Auth\User 作为 Authenticatable;

类用户扩展 Authenticatable { 使用 Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'first_name', 'last_name', 'user_name','email', 'password', 'phone', 'location', 'sponsor', 'photo',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

}

我的迁移代码是:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('user_name');
        $table->string('email');
        $table->string('password');
        $table->string('phone');
        $table->string('location');
        $table->string('sponsor');
        $table->binary('photo')->nullable();
        $table->text('permissions')->nullable();
        $table->timestamp('last_login')->nullable();
        $table->timestamps();

        $table->engine = 'InnoDB';
        $table->unique('email');
    });
4

4 回答 4

3

如果您使用的是最新版本的 laravel,请转到 config/databse.php 并连接有mysql数组:-

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => false, 
        'engine' => null,
    ],

默认情况下,严格为真并设置为假。(mysql数组中的倒数第二个键)希望它有帮助!

于 2018-05-12T06:28:59.047 回答
2

一般错误:1364 字段“照片”没有默认值

此错误意味着photo关联表中有一个没有默认值的列,并且您没有在插入查询中包含该列。

要解决它,要么制作该列,要么NULLABLE传递一些值以插入该列。

于 2017-09-20T13:02:55.090 回答
0

请检查您的 mysql 版本,如果您有大于 5.6.39 的版本。请转到变量并找到 sql 模式并删除 STRICT_TRANS_TABLES在此处输入图像描述

于 2018-06-13T10:58:05.373 回答
0

你的问题在这里

$request['photo'] = $safeName

您不能直接将关联数组之类的值放入Request Class Object 其中,这就是为什么没有值的原因photo,并且它为 SQL 插入提供了错误。

你应该做这样的事情

$request->request->add(['photo' => $safeName]);

或更好的方法

$user = Sentinel::registerAndActivate(array_merge($request->all(),['photo' => $safeName]));
于 2017-09-20T13:35:34.603 回答