【发布时间】:2015-06-25 19:43:39
【问题描述】:
我制作了一个播种机,用于为我的应用创建管理员用户
我的种子类
use Illuminate\Database\Seeder;
use App\User;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call('UserTableSeeder');
User::create([
'email'=>'admin',
'username'=>'admin',
'password'=>bcrypt('admin'),
'name'=>'admin',
'type'=>'a',
'lastLogin'=>'test',
'permission'=>'1,1,1,1,1,1,1,1,1,1,1,1']);
Model::reguard();
}
}
它正在我的数据库中创建记录。但是,如果我将此记录用于AuthController,它会失败。所以尝试像这样使用route.php闭包创建记录
Route::get('install',function(){
return \App\User::create([
'email'=>'admin',
'username'=>'admin',
'password'=>bcrypt('admin'),
'name'=>'admin',
'type'=>'a',
'lastLogin'=>'test',
'permission'=>'1,1,1,1,1,1,1,1,1,1,1,1']);
});
这对AuthController 非常有效。因此,为了找出问题,我尝试用这个哈希替换种子创建记录的哈希。然后它运作良好。为什么 bcrypt() 哈希都不同,即使两者都使用相同的随机字符串作为 salt ?
用户表迁移
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('name');
$table->string('email');
$table->string('password', 60);
$table->enum('type',['a','m','u']);
$table->string('lastLogin');
$table->string('permission');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
.env 文件
APP_ENV=local
APP_DEBUG=true
APP_KEY=aL3s6hAk375ogGSJQVKVB1r3Jf6OHZ5j
DB_HOST=localhost
DB_DATABASE=mymoney
DB_USERNAME=root
DB_PASSWORD=vip12340
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
config\app.php
return [
'debug' => env('APP_DEBUG'),
'url' => 'http://localhost',
'timezone' => 'UTC',
'locale' => 'en',
'fallback_locale' => 'en',
// I tried both ways
// 'key' => env('APP_KEY', 'SomeRandomString'),
'key' => env('APP_KEY', 'aL3s6hAk375ogGSJQVKVB1r3Jf6OHZ5j'),
'cipher' => 'AES-256-CBC',
'log' => 'single',
// All default providers and aliases
];
我使用php artisan serv 作为所有测试的网络服务器和laravel 5.1版。对不起,很长的问题
【问题讨论】:
标签: php hash laravel-5 seeding