jeudi 26 novembre 2015

Laravel 5 - creating two tables during registration

In my database design, I have two tables, People & Auth. The Auth table holds authentication information and the person_id while the People table holds all other information (name, address, etc). There is a one-to-one relationship between the tables as seen in the models below.

The reason I have separated the data into two tables is because in my application, I will have many people who do not have authentication capabilities (customers to the user).

App/Auth.php

   class Auth extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;

        public function person() {
            $this->belongsTo('Person');
        }
    }

App/Person.php

class Person extends Model
{
    public function auth() {
        $this->hasOne('Auth');
    }
}

In my AuthController::create() method, I am attempting to populate both models with the user supplied information like this:

protected function create(Request $request)
    {
        $person = \App\Person::create($request->all());

        $auth = new \App\Auth;
        $auth->fill($request->all());
        $auth->person_id = $person->id;
        $auth->save();

        return $person;
    }

In my application, I would like to authorize a user and pass a $user object as the authenticated person to subsequent routes. Am I doing this correctly? Is this the best way? There's cookies and bonus points if you can also explain how to retrieve the $user object after authentication...Auth table data is not needed in the $user object.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire