Unknown column ‘username’ in Laravel

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, email, updated_at, created_at) values (tony,
tony_admin, $2y$10$lBDvjtEu.tYueU
AuRutTteHLmOntXSBAZsX.tO.ZK3RtxwiLXOBGW, [email protected],
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' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b4afaeb99fa1a4ada9ae80aca1b2a1b6a5aceea3afad">[email protected]</a>'
        ));
    }
}

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 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

Method 3

Perhaps you can use this :

    DB::table('users')
    ->insert([
        'name' => 'tony',
        'username' => 'tony_admin',
        'password' => Hash::make('admin'),
        'email' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2f343522043a3f3632351b373a293a2d3e3775383436">[email protected]</a>'
    ]);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x