6

私は Laravel 4 を使用しており、一部のユーザーでデータベースをシードしようとしています。私はZizaco Confideプラグインを使用しているため、私のUserモデルはドキュメントConfideUserではなく拡張されEloquentています。モデルにもいくつかのフィールドを追加しましたが、それほど複雑なものはありません。これらのフィールドを削除しようとしましたが、同じ問題が発生します。

で呼び出して実行するUserSeederクラスを作成しました。エラーなしで実行され、「データベースがシードされました」が返されます。これは、を除くすべてのテーブルに当てはまります。ユーザーが挿入されることはありません。同様にユーザーを作成しようとしましたが、同じ結果が得られました。エラーはダンプされず、システムで見つけることができるログには何もありません。UserSeeder->run() メソッドにいくつかの var_dumps を挿入すると、オブジェクトが正しい値で作成されていることがわかりますが、何も保存されません。DatabaseSeederphp artisan migrate:refresh --seedusersUser::create(array(...))$user = new User ... $user->save()

私は何が欠けていますか?ここにいくつかのコードサンプルがあります。必要に応じてさらに提供できます。

モデル\User.php:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Zizaco\Confide\ConfideUser;

//class User extends Eloquent implements UserInterface, RemindableInterface {
class User extends ConfideUser
{
    // for Entrust
    use \Zizaco\Entrust\HasRole;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    public function agency()
    {
        if ($this->agency_type == 'local')
        {
            return $this->hasOne('Local');
        }

        if ($this->agency_type == 'county')
        {
            return $this->hasOne('County');
        }
    }

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

}

データベース\移行\xxxxxxxxx_confide_setup_users_table.php:

<?php
use Illuminate\Database\Migrations\Migration;

class ConfideSetupUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Creates the users table
        Schema::create('users', function($table)
        {
            $table->increments('id');
            $table->string('username');
            $table->string('email');
            $table->string('password');
            $table->string('confirmation_code');
            $table->boolean('confirmed')->default(false);
            $table->string('address1');
            $table->string('address2')->nullable();
            $table->string('state', 2);
            $table->string('zipcode', 9);
            $table->string('phone', 10);
            $table->string('extension',5 )->nullable();
            $table->string('fax', 10)->nullable();
            $table->enum('agency_type', array('local', 'county', 'state'))->default('local');
            $table->integer('agency')->unsigned()->nullable();
            $table->dateTime('last_seen');
            $table->timestamps();
            $table->softDeletes();
        });

        // Creates password reminders table
        Schema::create('password_reminders', function($t)
        {
            $t->string('email');
            $t->string('token');
            $t->timestamp('created_at');
        });
    }

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

}

データベース\シード\UserSeeder.php:

<?php

use \Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();

        User::create(
            array(
                'username' => 'local_test',
                'email' => 'nathan@abc.com',
                'password' => Hash::make('local'),
                'confirmation_code' => '483JU3ID8',
                'confirmed' => true,
                'address1' => '123 Main St.',
                'state' => 'MI',
                'zipcode' => '12345',
                'phone' => '5559993436',
                'agency_type' => 'local',
                'agency' => null,
                'last_seen' => new DateTime
            )
        );
4

3 に答える 3

4

ユーザー シーダー クラスでこれを行います。

class UsersTableSeeder extends Seeder
{
    public function run()
    {

     DB::table('users')->truncate();



        $users = array(
        array(  'username' => 'local_test',
            'email' => 'nathan@abc.com',
            'password' => Hash::make('local'),
            'confirmation_code' => '483JU3ID8',
            'confirmed' => true,
            'address1' => '123 Main St.',
            'state' => 'MI',
            'zipcode' => '12345',
            'phone' => '5559993436',
            'agency_type' => 'local',
            'agency' => null,
            'last_seen' => new DateTime
                )
        );

             // make sure you do the insert
         DB::table('users')->insert($users);

}
}

次に、 DatabaseSeeder.php ファイルでそれを呼び出すようにしてください

<?php

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {           
        $this->call('UsersTableSeeder');
    }

}
于 2013-10-17T17:31:47.180 に答える
1

Confide は検証に Ardent を使用します。「password_confirmation」プロパティを追加する必要があります。また、Confide が処理してくれるので、Hash::make も必要ありません。

<?php

class UsersTableSeeder extends Seeder
{
    public function run()
    {

        DB::table('users')->truncate();

        $users = array(
        array(  'username' => 'local_test',
            'email' => 'nathan@abc.com',
            'password' => 'local',
            'password_confirmation' => 'local',
            'confirmation_code' => '483JU3ID8',
            'confirmed' => true,
            'address1' => '123 Main St.',
            'state' => 'MI',
            'zipcode' => '12345',
            'phone' => '5559993436',
            'agency_type' => 'local',
            'agency' => null,
            'last_seen' => new DateTime
                )
        );

         // make sure you do the insert
         DB::table('users')->insert($users);

    }
}
于 2014-04-25T19:39:27.653 に答える