现在试图解决这个问题2天了。
当我尝试将用户数据播种到我的数据库时,我得到:
SQLSTATE [42S22]:未找到列:1054未知列'用户名'在 '字段列表' (SQL:插入
users
(name
,username
,password
,updated_at
,created_at
)值(tony, tony_admin,$ 2y $ 10 $ lBDvjtEu.tYueU
AuRutTteHLmOntXSBAZsX.tO.ZK3RtxwiLXOBGW,tony_admin @ laravel.com, 2018-02-18 00:12:39,201-02-18 00:12:39))
user.php的
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'username', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
创建用户迁移文件
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('username')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
注册控制器
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
数据库播种机
class DatabaseSeeder extends Seeder
{
public function run()
{
Model::unguard();
$this->call(UsersTableSeeder::class);
$this->command->info("Users table seeded :)");
Model::reguard();
}
}
class UsersTableSeeder extends Seeder
{
public function run()
{
User::create(array(
'name' => 'tony',
'username' => 'tony_admin',
'password' => Hash::make('admin'),
'email' => 'tony_admin@laravel.com'
));
}
}
我已尝试搜索答案,但所有类似的问题似乎都在尝试使用外键,而我只是尝试将username
字段添加到默认用户类。< / p>
我使用的是Laravel版本5.5.34。这里发生了什么?
答案 0 :(得分:2)
人们一直用错误的答案回答这个问题,所以我要发表我在评论中得到的答案。我试图将username
列添加到已运行的同一个迁移文件中。我需要创建一个新的迁移文件来更新users
表并添加username
列。迁移文件只能在laravel中运行一次。
答案 1 :(得分:-1)
在CMD中全部运行
php artisan view:clear
php artisan cache:clear
php artisan debugbar:clear
composer dump-autoload
php artisan log:clear
答案 2 :(得分:-1)
也许您可以使用它:
DB::table('users')
->insert([
'name' => 'tony',
'username' => 'tony_admin',
'password' => Hash::make('admin'),
'email' => 'tony_admin@laravel.com'
]);