Ich benutze Laravel 5.2 und Zizaco/entrust
, meine Frage ist:
Im folgenden Beispiel, wie Benutzerrolle speichern?Laravel 5.2: Wie kann man die Rolle des Benutzers in diesem Beispiel speichern?
** Registrierungsformular **, das auf Basis des Out-of-the-Box-Formulars ein Role
Element hinzugefügt wurde.
<form id="register" class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
{!! csrf_field() !!}
<fieldset class="form-group">
<label class="col-md-4 form-control-label">Role</label>
<div class="col-md-6">
<label class="c-input c-radio">
<input id="role1" name="role" type="radio" value="1">
<span class="c-indicator"></span>
red team
</label>
<label class="c-input c-radio">
<input id="role2" name="role" type="radio" value="2">
<span class="c-indicator"></span>
blue team
</label>
</div>
</fieldset>
<fieldset class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
<label class="col-md-4 form-control-label">username</label>
<div class="col-md-6">
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span class="help-block"><strong>{{ $errors->first('name') }}</strong></span>
@endif
</div>
</fieldset>
<fieldset class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
<label class="col-md-4 form-control-label">email</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
@endif
</div>
</fieldset>
<fieldset class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
<label class="col-md-4 form-control-label">password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
@endif
</div>
</fieldset>
<fieldset class="form-group{{ $errors->has('password_confirmation') ? ' has-danger' : '' }}">
<label class="col-md-4 form-control-label">password confirmation</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
@if ($errors->has('password_confirmation'))
<span class="help-block"><strong>{{ $errors->first('password_confirmation') }}</strong></span>
@endif
</div>
</fieldset>
<fieldset class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">submit</button>
</div>
</fieldset>
</form>
AuthController out-of-the-Box.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Ich kann einen RolesController machen und eine attachRole() Funktion schreiben, also kann ich manuell eine Rolle einen Benutzer anhang. dies wie:
public function attachRole()
{
$user = User::where('name', '=', 'foo')->first();
$user->roles()->attach(2);
return "attachRole done";
}
Jetzt möchte ich die Rolle speichern, die von dem Register Form empfangen wurde, aber nicht eine Rolle über attachRole() oben befestigen.
Wie ändert man die create
Funktion in AuthController?