0

I tried to modify the 'RegisterController' to fit my needs.

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;
    }
}

but it gives me this error upon sending a registration request from the registration form in the client.

MassAssignmentException in Model.php line 445: username

any ideas, help please?

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • Does the email column exist in the users database? The error indicates it doesn't. – Shane Nov 02 '16 at 03:39
  • there's no email in the 'users' table but in 'profile' table there's an email, you can check this part 'profile::create' – Juliver Galleto Nov 02 '16 at 03:44
  • it seems like that error is referring from the validation data – Juliver Galleto Nov 02 '16 at 03:50
  • @Shane: I have fixed the error and unfortunately it throws me another error 'MassAssignmentException in Model.php line 445: username' any, help please? I have updated my post – Juliver Galleto Nov 02 '16 at 04:04
  • Possible duplicate of [Laravel 5 : MassAssignmentException in Model.php](http://stackoverflow.com/questions/34565515/laravel-5-massassignmentexception-in-model-php) – Moppo Nov 02 '16 at 08:02

1 Answers1

1

As you mentioned that there is no email column in users table, update your validation method as below:

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

That means 'email' => 'required|email|max:255|unique:profile', instead of 'email' => 'required|email|max:255|unique:users'.

And to get rid of your MassAssignmentException in your profile model class use $fillable property.

protected $fillable = ['username', 'first_name', 'last_name', 'email'];
Mahfuzul Alam
  • 3,057
  • 14
  • 15
  • Yeah, I figured it out. I have fixed the error and unfortunately it throws me another error 'MassAssignmentException in Model.php line 445: username' any, help please? I have updated my post. – Juliver Galleto Nov 02 '16 at 04:04
  • In your profile model class use $fillable property `protected $fillable = ['column1', 'column2', 'etc.'. '...']`. Replace column names with your profile table columns except `id, created_at and updated_at`. Hope it helps. – Mahfuzul Alam Nov 02 '16 at 04:09
  • any explanation why i have to add $fillable Sir? – Juliver Galleto Nov 02 '16 at 04:14
  • According to laravel docs, "$fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable. So, $guarded functions like a "black list". Of course, you should use either $fillable or $guarded - not both." Basically it is to protect your fields. – Mahfuzul Alam Nov 02 '16 at 04:17