问题
Been trying to solve this for 2 days now.
When I try to seed user data to my database, I get:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'field list' (SQL: insert into
users(name,username,password,updated_at,created_at) values (tony, tony_admin, $2y$10$lBDvjtEu.tYueU
AuRutTteHLmOntXSBAZsX.tO.ZK3RtxwiLXOBGW, tony_admin@laravel.com, 2018-02-18 00:12:39, 2018-02-18 00:12:39))
User.php
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'username', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
Create users migration file
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');
}
}
Register controller
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']),
]);
}
}
Database seeders
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'
));
}
}
I've tried searching for answers, but all the similar questions seem to trying to use foreign keys, whereas I'm just trying to add a username field to the default user class.
I'm using Laravel version 5.5.34. What's going on here?
回答1:
People keep answering this with wrong answers so I'm going to post the answer that I got in the comments which worked. I was trying to add the username column with the same migration file which had already ran. I needed to create a new migration file to update the users table and add the usernamecolumn. Migration files can only be run once in laravel.
回答2:
Run this all in CMD
php artisan view:clear
php artisan cache:clear
php artisan debugbar:clear
composer dump-autoload
php artisan log:clear
回答3:
Perhaps you can use this :
DB::table('users')
->insert([
'name' => 'tony',
'username' => 'tony_admin',
'password' => Hash::make('admin'),
'email' => 'tony_admin@laravel.com'
]);
来源:https://stackoverflow.com/questions/48847321/unknown-column-username-in-laravel