首页 > 解决方案 > 为什么在我提交注册表时它给我错误

问题描述

这是错误者:

Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where 子句' (SQL: select count(*) as aggregate from userswhere `` = user@email.com)

我的注册控制器:

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
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'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);
}

的用户表迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email', 150)->unique();
            $table->string('password');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('number')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

**My User Model**
<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name','email','password',];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

**My View File Register.blade.php**
<!DOCTYPE html>
<html>

<head>

</head>

<body>
    <div class="accountbg"></div>
    <div class="wrapper-page">
        <div class="panel panel-color panel-primary panel-pages">
            <div class="panel-body">
                <h3 class="text-center m-t-0 m-b-30">
                    <span class=""><img src="{{ asset('/') }}admin/assets/images/
                    logo_dark.png" alt="logo" height="32"></span>
                </h3>
                <h4 class="text-muted text-center m-t-0"><b>{{ __('Sign Up') }}</b></h4>
                <form class="form-horizontal m-t-20" method="POST" action="{{ route('register') }}" autocomplete="on">
                @csrf
                    <div class="form-group">
                        <div class="col-xs-12">
                            <input class="form-control" name="name" required="required" type="text" placeholder="Username"
                            value="{{ old('name') }}" autocomplete="off" autofocus/>

                            @error('name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <input class="form-control" name="email"
                            value="{{ old('email') }}" required="required" type="email" placeholder="Email" autocomplete="email"/>
                            @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <input class="form-control" name="password" required="required" type="password"
                            placeholder="Password" autocomplete="off"/>
                            @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <input class="form-control" name="password_confirmation" required="required" type="password"
                            placeholder="Confirm password" autocomplete="password"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-12">
                            <div class="checkbox checkbox-primary">
                                <input id="checkbox-signup" name="term" type="checkbox"
                                value="{{ old('term') ? 'checked' : '' }}">
                                <label for="checkbox-signup"> {{ __('I accept') }} <a href="{{ route('register') }}">
                                    {{ __('Terms and Conditions') }}
                                </a> </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group text-center m-t-20">
                        <div class="col-xs-12">
                            <button class="btn btn-primary w-md waves-effect waves-light" type="submit">{{ __('Register') }}</button>
                        </div>
                    </div>
                    <div class="form-group m-t-30 m-b-0">
                        <div class="col-sm-12 text-center"> <a href="{{ route('login') }}"
                            class="text-muted">{{ __('Already have account?') }}</a></div>
                    </div>
                </form>
            </div>
        </div>
    </div>

</body>
</html>

标签: phplaravellaravel-validation

解决方案


你应该像这样写独特的规则,你有一个逗号。

'email' => ['unique:users',],

一个很好的选择是为此使用验证规则类,然后您将获得参数类型提示并知道有哪些选项。

'email' => [new Unique('users'),],

推荐阅读