dongle7553 2016-05-12 13:46
浏览 31

Laravel 5.2:如何在此示例中保存用户的角色?

I am using Laravel 5.2 and Zizaco/entrust,my question is:
In the following example,how to save user's role?

**register form ** which was added a Role item on the basis of the out-of-the-box form.

<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']),
    ]);
}

I can make a RolesController and write a attachRole() function,Thus,I can manually attach a role to a user. like this:

public function attachRole()
{
    $user = User::where('name', '=', 'foo')->first();
    $user->roles()->attach(2);
    return "attachRole done";
}

Now, I want to save the role that was received from register form,but not attach a role via attachRole() above.
How to modify the create function in AuthController?

  • 写回答

1条回答 默认 最新

  • dqr91899 2016-05-12 13:59
    关注

    You can do it with the Authorization functionality in Laravel, https://laravel.com/docs/5.2/authorization.

    Example code:

    $gate->before(function ($user, $ability) {
        if ($user->isSuperAdmin()) {
            return true;
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?