Laravel Ardent不让我保存我的用户


Laravel Ardent not letting me save my User

我昨晚在Laravel启动了一个项目,正在使用Ardent和Entrust包来帮助使我的用户模型更加安全和易于使用。我已经设置了所有内容,但无法为我的数据库播种。没有抛出任何错误,但它肯定不会保存到我的数据库中。

这是我的用户模型。

<?php
use Illuminate'Auth'UserInterface;
use Illuminate'Auth'Reminders'RemindableInterface;
use LaravelBook'Ardent'Ardent;
use Zizaco'Entrust'HasRole;
class User extends Ardent implements UserInterface, RemindableInterface {
    use HasRole;
    public static $rules = array(
        'username'              => 'required|alpha_num|between:6,18|unique:users',
        'email'                 => 'required|email|unique:users',
        'password'              => 'required|alpha_num|min:8',
        'password_confirmation' => 'required|alpha_num|min:8',
        'first_name'            => 'alpha',
        'last_name'             => 'alpha',
        'city'                  => 'alpha',
        'state'                 => 'alpha',
        'rate'                  => 'numeric',
        'profile_pic'           => 'image',
        'commitment'            => 'string'
    );
    public static $passwordAttributes  = array('password');
    public $autoHydrateEntityFromInput = true;
    public $forceEntityHydrationFromInput = true;
    public $autoPurgeRedundantAttributes = true;
    public $autoHashPasswordAttributes = true;
    protected $table = 'users';
    protected $hidden = array('password');
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }
    public function getAuthPassword()
    {
        return $this->password;
    }
    public function getReminderEmail()
    {
        return $this->email;
    }
}

这是我迁移中的users模式。

Schema::create(
    'users',
    function (Blueprint $table) {
        $table->increments('id');
        $table->string('username');
        $table->string('email');
        $table->string('password', 60);
        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('city')->nullable();
        $table->string('state', 2)->nullable();
        $table->string('zip', 9)->nullable();
        $table->float('rate', 10, 2)->nullable();
        $table->boolean('listed')->default(false);
        $table->string('profile_pic')->nullable();
        $table->string('commitment')->nullable();
        $table->timestamps();
        $table->softDeletes();
    }
);

最后,这是我的种子数据。

$admin = User::create(
    array(
         'username'              => 'admin',
         'email'                 => 'admin@example.com',
         'password'              => 'admin',
         'password_confirmation' => 'admin',
         'first_name'            => 'Admin',
         'last_name'             => 'User',
         'city'                  => 'Cincinnati',
         'state'                 => 'OH',
         'zip'                   => '45243'
    )
);
User::create(
    array(
         'username'              => 'user',
         'email'                 => 'user@example.com',
         'password'              => 'user',
         'password_confirmation' => 'user',
         'first_name'            => 'Normal',
         'last_name'             => 'User'
    )
);

我已经把它缩小到我在用户模型中扩展Ardent的事实,但我不知道为什么。

我发现了这个问题。我的种子没有通过验证。因为当它失败时,Ardent将错误存储在模型中的MessageBag实例中,我没有看到错误。我不得不把这些错误打印在我的种子上才能看到。真是个愚蠢的错误。希望其他人看到这一点,不要犯那个错误!

通常情况下,您只需将种子数据直接插入数据库,实际上并不使用您的模型:

$user = array (
         'username'              => 'admin',
         'email'                 => 'admin@example.com',
         'password'              => Hash::make('admin'),
         'first_name'            => 'Admin',
         'last_name'             => 'User',
         'city'                  => 'Cincinnati',
         'state'                 => 'OH',
         'zip'                   => '45243');
DB::table('users')->insert($user);

我知道已经晚了,你已经解决了这个问题,但我想我应该为其他人添加一个注释。。。

您的密码验证规则规定密码必须至少为8个字符,而您的种子数据只有5个('admin')

这只是发生在我身上,我意识到我的数据库没有我存储的值。它没有抛出和异常,而是变为空白,没有执行下一段代码。这很奇怪,但这可能是一个解决方案。