greg5737's avatar

correcting a misspelled name in tinker

I am doing the laravel 5 from scratch #10 and adding a user to the user table. I misspelled the field name "password" as "pasword": Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Un known column 'pasword' in 'field list' (SQL: insert into users I corrected it by retyping $user->password = bcrypt('password'); but the wrong entry still existed so when I tried to save it I got the same error. How do I remove the wrong spelling from tinker without having to start completely over.

0 likes
16 replies
willvincent's avatar

php artisan tinker

DB::statement('ALTER TABLE users RENAME COLUMN pasword TO password');

greg5737's avatar

Thank you for the help. I got an error using your code. I am giving you more info below than I did the first time. See where I created the info, spelled password wrong, tried to correct it and the error. The name of the field in my users table is correct. I just misspelled password when creating the user info.

$user = new App\User; => App\User {#667} $user->username = 'Greg'; => "Greg" $user-email = '[email protected]'; PHP Parse error: Syntax error, unexpected '=' on line 1 $user->email = '[email protected]'; => "[email protected]" $user->pasword =bcrypt('password'); => "$2y$10$QZJx4RMl.QyvS8d9R8dgLO.ul9ipsp4qfmhJNxTOM8TpIMpfq2Ste" $user->save(); Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not fo und: 1054 Unknown column 'pasword' in 'field list' (SQL: insert into users (username, email, pasword, updated_at, created_at) values (Greg, [email protected], $2y$10 $QZJx4RMl.QyvS8d9R8dgLO.ul9ipsp4qfmhJNxTOM8TpIMpfq2Ste, 2016-11-02 12:30:24, 2016-11-02 12:30:24))' DB::statement('ALTER TABLE users RENAME COLUMN pasword TO password'); Illuminate\Database\QueryException with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that c orresponds to your MariaDB server version for the right syntax to use near 'COLUMN pasw ord TO password' at line 1 (SQL: ALTER TABLE users RENAME COLUMN pasword TO password)'

willvincent's avatar

So is the column in the database pasword or password ? If the column name is correct, why are you setting the correct spelling of the password (the value) to the misspelled column name?

Snapey's avatar

Why not just open another window or whatever, fix your table column name with migration or directly with your favourite tool, switch back to Tinker, press up arrow and resubmit the command?

greg5737's avatar

I am new at this so this is what I think is happening. I open tinker and create user info. One of the pieces is the password. The column in the table is spelled 'password'. However, I accidentally spell it wrong using tinker: $user->pasword =bcrypt('password'); I hit enter and it creates a crypted password. Once done creating user info I do the save command $user->save(); and i get the message that I spelled password wrong. I assume it is trying to append the data I created into the table and the variable i created does not match the table. Is there a way to correct the variable 'pasword' to 'password' without exiting tinker and starting over again. I can type it correctly but I still get the error because the first time I typed it wrong and it is still in the memory. How to I remove it from the tinker memory? I am looking for something like $user->pasword->REMOVE.

greg5737's avatar

I will give that a try in about an hour. Thank you so much.

Snapey's avatar

huh? you just press up arrow, backspace over the command, correct it and press enter?

Even if you had to quit tinker you could copy and paste the commands between sessions.

Snapey's avatar

Here; your commands so that you can cut and paste them into tinker

$user = new App\User;

$user->username = 'Greg';

$user->email = '[email protected]';

$user->password =bcrypt('password');

$user->save();

willvincent's avatar

I'm still confused as to what the issue really is here.. so providing a definitive answer is not really possible. I agree though, just fix the problem and resubmit the command.. up arrow or otherwise.

And, sometimes exiting tinker is necessary -- like if you've made changes to code, tinker doesn't re-evaluate everything, so .. config changes and such for example, made outside of tinker, will require exiting out of tinker and restarting it to take effect. At least that's been my experience.

greg5737's avatar

I think that pressing up arrow and correcting would be the same as just retyping it. I tried that and still got the error because the old misspelled word was still in the tinker memory (I am just guessing at that). On your second suggestion, I seem to recall that when I control C and quit tinker and then go back in and try to up arrow, none of my previous commands are there. Am I doing it wrong?

greg5737's avatar

Snapey, am I understanding Tinker correctly? When I type $user->username = 'greg'; and then hit enter, it is saving greg in the variable username....and this is the questionable part....inside a temporary memory in Tinker. Then when I do the save command it is appending those variables into the corresponding field names in the table?

Snapey's avatar

You are running your php application with tinker providing a cli

You create a user object, you then set the attributes and save. Yes if you just redo the command then you will have pasword attribute and password attribute. Save is then not possible.

But, $user = new App\User just starts afresh and you repeat the exercise

greg5737's avatar

Got it. And yes, new App\User would allow the up arrow to supply previous commands. Perfect. Thank you.

Please or to participate in this conversation.