mardi 17 mai 2016

Laravel 5.1 - Registration

I'm building a simple user registration on Laravel 5.1 and it's really driving me crazy.

I started from the default Laravel built-in registration system and added some extra fields.

My case involves the following files:

routes.php

// Registration routes...

Route::get('auth/register', function(){
    $organisations = Organization::all();

    return view('auth.register', ['organizations' => $organisations]);
});

Route::post('auth/register', 'Auth\AuthController@postRegister');

User.php

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

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['organization_id', 'team_id', 'lastname', 'firstname', 'login', 'password'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

/**
 * Get the user's organisation.
 */
public function organization(){
    return $this->belongsTo('App\Organization');
}

/**
 * Get the user's team.
 */
public function team(){
    return $this->belongsTo('App\Team');
}
}

Organization.php

class Organization extends Model
{
protected $table   = 'organizations';
public $timestamps = false;

protected $fillable = ['name'];

/**
 * Get the authors for the organization.
 */
public function users()
{
    return $this->hasMany('App\Users');
}

/**
 * Get the teams for the organization.
 */
public function teams()
{
    return $this->hasMany('App\Teams');
}

}

Team.php

class Team extends Model
{
protected $table   = 'teams';
public $timestamps = false;

/**
 * Get the team's organisation
 */
public function organisation()
{
    return $this->belongsTo('App\Organisation');
}

/**
 * Get the authors for the team.
 */
public function users()
{
    return $this->hasMany('App\Users');
}

}

AuthController.php

/
/.......
/......
/

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'firstname'     => 'required|max:255',
        'lastname'      => 'required|max:255',
        'login'         => 'required|max:255',
        'organization'  => 'required|max:255',
        'team'          => 'required|max:255',
        'password'      => 'required|confirmed|min:4',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'firstname'       => $data['firstname'],
        'lastname'        => $data['lastname'],
        'login'           => $data['login'],
        'organization_id' => $data['organization'],
        'team_id'         => $data['team'],
        'password'        => bcrypt($data['password']),
    ]);
}

I won't post my view code, my inputs are correct.

My problem is that I can store only ONE new user.

What's happening is:

  1. my users table is empty
  2. I fill up my form and then submit it to create my first user
  3. it works (yay), I am correctly redirected to my home page

  4. I want to create a second user

  5. The new user isn't stored in my table
  6. I am redirected to the home page however

If I delete my user entry in my table (so it means I empty my table), then I can create a new user again. But I can ALWAYS have one and only one entry. I can't add more.

Just out of curiosity I tried this in my routes file:

Route::get('/lol', function () {
    return User::create([
        'firstname'       => 'what',
        'lastname'        => 'the',
        'login'           => 'f***',
        'organization_id' => 1,
        'team_id'         => 4,
        'password'        => 'blabla',
    ]);
});

and of course it works like a charm each time I am calling the route /lol

So what the hell is going on in this AuthController? Am I out of my mind?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire