【问题标题】:i'm noobie need help xD i have error in sql but i dont know where. laravel 7 [closed]我是新手,需要帮助 xD 我在 sql 中有错误,但我不知道在哪里。 laravel 7 [关闭]
【发布时间】:2020-10-14 01:33:56
【问题描述】:

我想刷新迁移时出现此错误

Illuminate\Database\QueryExceptionSQLSTATE[42S21]:列已存在:1060 列名重复“created_at”(SQL:创建表 users (id bigint unsigned not null auto_increment 主键, firstname varchar(255) not null, email varchar(255) not null, email_verified_at timestamp null, password varchar(255) not null, @987654327 @ varchar(255) 不为空,created_at 日期不为空,addressvarchar(255) 不为空,cityvarchar(255) 不为空,zipcodevarchar(255) 不为空,gendervarchar( 255) 不为空,number int 不为空,birthday 日期不为空,pseudo varchar(255) 不为空,remember_token varchar(100) 为空,created_at 时间戳为空,updated_at 时间戳为空)默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

我试图找到,但我不知道在哪里,我是菜鸟,需要一点帮助。

用户.php

命名空间应用程序; 使用 Illuminate\Contracts\Auth\MustVerifyEmail; 使用 Illuminate\Foundation\Auth\User 作为 Authenticatable; 使用 Illuminate\Notifications\Notifiable; 类用户扩展 Authenticatable { 使用通知; /** * 可批量分配的属性。 * * @var 数组 */ 受保护的 $fillable = [ 'name', 'birthday', 'gender', 'email', 'password', 'firstname', 'address', 'city', 'zipcode', 'gender', 'number', 'pseudo', ]; /** * 应该为数组隐藏的属性。 * * @var 数组 */ 受保护的$隐藏= [ '密码','remember_token', ]; /** * 应该转换为本机类型的属性。 * * @var 数组 */ 受保护的 $casts = [ 'email_verified_at' => '日期时间', ]; }

这是创建用户

使用 Illuminate\Database\Migrations\Migration; 使用 Illuminate\Database\Schema\Blueprint; 使用 Illuminate\Support\Facades\Schema; 类 CreateUsersTable 扩展了迁移 { /** * 运行迁移。 * * @return 无效 */ 公共函数 up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('名字'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('密码'); $table->string('name'); $table->date('created_at'); $table->string('地址'); $table->string('city'); $table->string('zipcode'); $table->string('性别'); $table->integer('number')->unique(); $table->date('生日'); $table->string('伪')->unique(); $table->rememberToken(); $table->timestamps(); }); } /** * 逆转迁移。 * * @return 无效 */ 公共函数向下() { Schema::dropIfExists('users'); $table->dropColumn('gender'); } }

还有控制器注册

命名空间 App\Http\Controllers\Auth; 使用 App\Http\Controllers\Controller; 使用 App\Providers\RouteServiceProvider; 使用应用\用户; 使用 Illuminate\Foundation\Auth\RegistersUsers; 使用 Illuminate\Support\Facades\Hash; 使用 Illuminate\Support\Facades\Validator; 类 RegisterController 扩展控制器 { /* |------------------------------------------------- ------------------------- |注册控制器 |------------------------------------------------- ------------------------- | |该控制器处理新用户的注册以及他们的 |验证和创建。默认情况下,此控制器使用 trait 来 |无需任何额外代码即可提供此功能。 | */ 使用注册用户; /** * 注册后将用户重定向到哪里。 * * @var 字符串 */ 受保护的 $redirectTo = RouteServiceProvider::HOME; /** * 创建一个新的控制器实例。 * * @return 无效 */ 公共函数 __construct() { $this->middleware('guest'); } /** * 为传入的注册请求获取验证器。 * * @param 数组 $data * @return \Illuminate\Contracts\Validation\Validator */ 受保护的函数验证器(数组 $data) { 返回验证器::make($data, [ 'name' => ['required', 'string', 'max:255'], 'firstname' => ['required', 'string', 'max:255'], '地址' => ['必需', '字符串', 'min:8'], 'city' => ['required', 'string', 'min:8'], 'zipcode' => ['required', 'string', 'min:8'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], '生日' => ['必需|日期格式:Y-m-d|之前:今天'], '密码' => ['必需', '字符串', 'min:8', '确认'], '伪' => ['required', 'string', 'min:8', 'unique:users'], 'number' => ['required', 'integer', 'min:8', 'unique:users'], '性别' => ['必需', '字符串', 'min:8'], ]); } /** * 有效注册后创建一个新的用户实例。 * * @param 数组 $data * @return \App\用户 */ 受保护的函数创建(数组 $data) { 返回用户::创建([ 'name' => $data['name'], '名字' => $data['名字'], '地址' => $data['地址'], '邮编' => $data['邮编'], 'email' => $data['email'], '生日' => $data['生日'], '城市' => $data['城市'], '数字' => $data['数字'], '性别' => $数据['性别'], '伪' => $数据['伪'], '密码' => Hash::make($data['password']), ]); } }

register.blade.php

@section('内容')
{{ __('Register') }}
@csrf
@错误('电子邮件') {{ $message }} @enderror
@error('密码') {{ $message }} @enderror
@error('名字') {{ $message }} @enderror
@error('名字') {{ $message }} @enderror
@error('地址') {{ $message }} @enderror
@error('城市') {{ $message }} @enderror
@error('邮政编码') {{ $message }} @enderror
@error('数字') {{ $message }} @enderror
@error('生日') {{ $message }} @enderror
女性
男性
@if ($errors->has('gender')) {{ $errors->first('gender') }} @万一
@endsection

有人看到哪里出错了吗?我需要离开这个我尝试学习laravel,在谷歌我没有找到错误

【问题讨论】:

    标签: php html mysql laravel


    【解决方案1】:

    问题在于您的用户表迁移。

    public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->id();
                $table->string('firstname');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->string('name');
                $table->date('created_at');
                $table->string('address');
                $table->string('city');
                $table->string('zipcode');
                $table->string('gender');
                $table->integer('number')->unique();
                $table->date('birthday');
                $table->string('pseudo')->unique();
                $table->rememberToken();
                $table->timestamps();
            });
        }
    

    您正在尝试创建一个列$table->date('created_at');,但您也有$table->timestamps();。这也试图创建一个名为created_at 的列。应该删除其中之一。

    如果您确实删除了 $table->timestamps();,您将没有 updated_at 列,您的模型将需要更新:

    public $timestamps = false;
    

    更新的用户模型

    namespace App;
    
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    
    class User extends Authenticatable
    {
        use Notifiable;
    
        public $timestamps = false;
    
        // Rest of your model
        ......
    

    最后一件事,在您的down 迁移函数中,您删除表,然后拥有$table->dropColumn('gender');。这可能会导致问题,因为您已经删除了整个表,因此已经删除了 gender 列。

    【讨论】:

    • 如何添加电台性别?用户在注册时可以选择是男是女
    猜你喜欢
    • 2011-01-30
    • 2021-09-13
    • 1970-01-01
    • 2022-01-21
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多