注册时将角色附加到用户laravel 5.4 [英] attach role to a user upon registration laravel 5.4

查看:46
本文介绍了注册时将角色附加到用户laravel 5.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在用户注册时将我的角色之一附加为用户的默认角色,我尝试了不同的方式,例如本文 https://linustechtips.com/main/topic/802733-laravel-54-attaching-role-on-registration/,但没有任何效果:

I try to attach one of my roles as default role for users when they register, I tried different ways such as this post https://linustechtips.com/main/topic/802733-laravel-54-attaching-role-on-registration/ but nothing works:

我尝试附加到注册表中的角色是 name =>用户 id =>3 ,如果您想帮助我进行编码

the role I try to attach to registration form is name => user and id => 3 in case you want to help me with coding

这是我的注册表:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Role;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * 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, [
            'fname' => 'required|string|max:255',
            'lname' => 'required|string|max:255',
            'username' => 'required|string|min:4|max:255|unique:users',
            'gender' => 'required',
            'city' => 'sometimes|string|max:255',
            'province' => 'sometimes|string|max:255',
            'country' => 'sometimes|string|max:255',
            'phone' => 'sometimes|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'fname' => $data['fname'],
            'lname' => $data['lname'],
            'username' => $data['username'],
            'gender' => $data['gender'],
            'city' => $data['city'],
            'province' => $data['province'],
            'country' => $data['country'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

谢谢.

推荐答案

您是否使用过 php artisan make:auth 来设置身份验证?

Did you use the php artisan make:auth to set up your authentication?

我已将以下内容添加到app \ Http \ Controllers \ Auth \ RegisterController.php中,以为所有用户分配默认角色"user"

I've added the following to the app\Http\Controllers\Auth\RegisterController.php to assign all users the default role of 'user'

protected function create(array $data)
{
    $role = Role::where('name', 'user')->first();

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    $user->assignRole($role);

    return $user;
}

以及User.php中的以下内容

And the following in User.php

public function roles()
{
    return $this->belongsToMany(Role::class);
}

public function assignRole(Role $role)
{
    return $this->roles()->save($role);
}

这篇关于注册时将角色附加到用户laravel 5.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆