Model.php第445行中的MassAssignmentException:username

时间:2016-11-02 03:30:17

标签: php laravel laravel-5.3

我试图修改' RegisterController'以满足我的需求。

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\profile;
use App\roles_id;
use App\permissions_id;
use Validator;
use App\Http\Controllers\Controller;
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 login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/app/system/dashboard';

    /**
     * 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, [
            'first_name' => 'string|required|max:255|unique:profiles',
            'last_name' => 'string|required|max:255',
            'email' => 'required|email|max:255|unique:profiles',
            'username' => 'required|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        //create user
        $user = User::create([
            'username' => $data['username'],
            'password' => bcrypt($data['password']),
            'real_password' => $data['password'],
        ]);
        //create profile
        profile::create([
            'username' => $data['username'],
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
        ]);
        //create roles
        roles_id::create([
            'role_id' => 1,
            'username' => $data['username'],
        ]);

        //create role
        roles_id::create([
            'role_id' => 1,
            'username' => $data['username'],
        ]);

        //create permission
        permisssions_id::create([
            'perm_id' => 0,
            'username' => $data['username'],
        ]);

        return $user;
    }
}

但是在从客户端的注册表单发送注册请求时,它会给我这个错误。

  

Model.php第445行中的MassAssignmentException:用户名

任何想法,请帮忙吗?

1 个答案:

答案 0 :(得分:1)

如您所述,email表中没有users列,请按以下方式更新验证方法:

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'first_name' => 'string|required|max:255',
            'last_name' => 'string|required|max:255',
            'email' => 'required|email|max:255|unique:profile',
            'username' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

这意味着'email' => 'required|email|max:255|unique:profile',而不是'email' => 'required|email|max:255|unique:users'

要在个人资料模型类中删除 MassAssignmentException ,请使用$fillable属性。

protected $fillable = ['username', 'first_name', 'last_name', 'email'];