How to Assign Role in Default AuthController

Posted 2 years ago by Khudadad

I want to assign a role to user during sign up in Laravel default AuthController, so how to do this if anyone idea? Thanks users migration:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });
}

user_roles migration:

public function up()
{
    Schema::create('user_roles', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->integer('role_id');
        $table->timestamps();
    });
}

roles migration:

 public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 60);
        $table->text('description');
        $table->timestamps();
    });
}

register.blade:

    ......
    <div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }}">
            <select name="role" id="role" class="form-control mb-10">
                <option value="" selected="selected">Select A Role</option>
                @foreach($roles as  $role)
                    <option value="{!! $role->r_id !!}">{!! $role->role_name !!}</option>
                @endforeach
            </select>
            @if ($errors->has('role'))
                <span class="help-block">
                    <strong>{{ $errors->first('role') }}</strong>
                </span>
            @endif
        </div>

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.