Posted by on 10/05/2014
The way I understand Laravel 4, every time you log out, using the Auth namespace, the function will then update the record of the Users table specifically the updated_at and remember_token attributes.

But then, what if the remember_token doesn't exists? Then logging out would return the following error.

SQLSTATE[42S22]:Column not found:1054 Unknown column 'remember_token' in 'field list'

How I arrive at this..

I'm converting a previous website from RoR to PHP using Laravel and I don't want to touch the database as it is right now. Since Laravel is also an MVC framework then the database should be almost the same as the other except for remember_token which seems to be required in the Users table (or at least the model) that you'll be using for authentication.

In return, when you use the Auth namespace, logging out will surely throw you an SQL error.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'remember_token' in 'field list'

Solution

One way to resolve this is to just do a migration that will add the missing column. But what if you have no access to the database (or like me that don't want to add a column for some unknown reason).

To solve this, we can just override the function by adding this line of code on your User model:

public function getRememberTokenName()
{
 return null; // not supported
}

/**
* Overrides the method to ignore the remember token.
*/
public function setAttribute($key, $value)
{
 $isRememberTokenAttribute = $key == $this->getRememberTokenName();
 if (!$isRememberTokenAttribute)
 {
  parent::setAttribute($key, $value);
 }
}

You can also set the getRememberToken attribute to return null but in my experience it does work even without doing it.

Since getRememberTokenName always returns null then every null value that needs to be set will be disregarded including the 'remember_token'. That means, 'remember_token' will not be touched on the database.


1 comment :