Last Updated: February 25, 2016
·
515
· naneri

Using Laravel on top of old database.

Recently I decided to use Laravel to work with my own project, which is based on a php CMS.
The reason of why is choose Laravel was that it gave me much easier approach towards adding functionality and maintaining the code base.

First you need to change the 'User' model in app\models\user.php file. You have to add the following lines to the model

protected $table = 'user';

protected $primaryKey = 'pk_i_id';

This will tell the model to use 'user' table (by default all tables inherit the name of the model in plural form, so by default it is 'users') and use "pkiid" as primary key (by default it is 'id')

public function getAuthPassword()
{
    return $this->s_password;
}

This method tells which field is used to store the password.

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

public function setRememberToken($value)
{
// not supported
}

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 should add this methods, in case you don't want to use 'remember me' functionality. They override the parent class methods. If you need such a functionality, you can add a 'remember_token' nullable string field in the database with length of 100 characters.

After these manipulations you should be able to develop a new Laravel app on top of your old database. Hope this tutorial was helpfull for you. If you notice any mistakes, pelase let me know.