質問

I started a project last night in Laravel and am using Ardent and Entrust packages to help make my User model more secure and easy to use. I have set everything up but am not able to seed my database. No errors are thrown but it is definitely not saving to my database.

This is my User model.

<?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;
    }

}

This is my users schema in my migrations.

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();
    }
);

Lastly, here is my seed data.

$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'
    )
);

I have narrowed it down to the fact that I'm extending Ardent in my User model, but I don't know why.

役に立ちましたか?

解決

I figured out the issue. My seeds weren't passing validation. Because when it fails Ardent stores the errors in a MessageBag instance in the model, I wasn't seeing the errors. I had to print those errors out in my seeds to see. Such a dumb mistake. Hopefully others see this and don't make that mistake!

他のヒント

Generally you just insert the seed data directly into the database, you dont actually use your model:

$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);

I know it's late and you've solved this, but thought I'd add a note for others...

Your password validation rules state that the password has to be a minimum of 8 characters, while your seed data only had 5 ('admin')

It just happened to me, and I realized that my db didn't have value I was storing. Instead of throwing and exception it just went blank and did not execute the next piece of code. It's weird, but that may be a solution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top