1

I have used laravel 5.2 default user registration and login Form. My requirement is creating two type of users. it is not kind of user and admin. But its two type of user in the website user1 and user2 having different fields in the registration form.So for this i have copied register.blad.php and create a function in authcontroller. For distinguishing two form values in database i have used a field named user_type. user_type is hidden field in the both registration form. here is my auth controller

   protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'user_type'=> $data['user_type'],
            'password' => bcrypt($data['password']),
        ]);
    }
/* custom function for user type */

    public function getRecruiter(){

        return view('auth.recruiter');
    }

    public function postRecruiter(Request $request){

               $this->validate($request, [
                'name' => 'required|max:255',
                'email' => 'required|email|unique:users',
                'password'=> 'required|min:6',
                'company'=>'required',
                'location'=>'required',
                'url'=>'required'
            ]);

           $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'user_type'=> $request->user_type, // user_type for second user type.
            'password' => bcrypt($request->password),
           ]);

           return view('home');

    }

every thing is working fine but user_type, remember_token is not saving in database.

Last is. I do not know how to login user after register. like default functionality. if i use Auth::login($user) then it is giving error.

App\Http\Controllers\Auth\auth not found.

Please help me solve this as I do not want to create any other table . As many thready were like having two tables but i want to solve it with one table with different user type.

2
  • check whether your $request variable in postRecruiter have user_type.
    – Abbasi
    May 17, 2016 at 12:56
  • yes, i have checked it and even i got value in it. i have use user_type as hidden field in register form
    – revtech
    May 18, 2016 at 4:09

1 Answer 1

2

I found the solution for not saving value in database. i was forgetting to add my variable name in protected fillable.


      protected $fillable = ['name', 'email', 'password','user_type',];

But now another point is left that is auto login user after registration as Auth::login is not working.

Edited: I have also solved Auth Issue.. I forget to add use Auth in my controller.

silly mistake

1
  • I always forget that dumb Mass Assigment stuff and brings me problems from time to time lol. Feb 5, 2021 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.