2

I'm slowing catching up with laravel but problem is, trying to register a new user into my new laravel application. the user role is not shown on the user page (index.blade.php) and I can't sign into the application using the username and password that is saved. The other problem i am facing is, the Role assigned to a user is not displayed either. What could be the problem and how do i encrypt the entered user password?

this is the store function

public function store(Request $request)
    {
        //validate data
        $validation=$this->validate($request, [
            'username' => 'required|max:50',
            'role_id' => 'required|numeric',
            'email' => 'required|email|max:50'
        ]);
        $user = new User;
        $user->username = $request->username;
        $user->role_id = $request->role_id;
        $user->email = $request->email;
        $user->save();
        // redirect
        Session::flash('message', 'Successfully added new user!');
        Session::flash('alert-type', 'success');
        return Redirect::to('user');
    }
Benson Okello
  • 354
  • 3
  • 12

3 Answers3

2

You should encrypt password by using bcrypt():

$user->password = bcrypt($request->password);
$user->save();

Then you'll be able to login.

As for role_id, your code is correct, you should just make sure role_id field is inside the form.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • yes it is included as a foreign key that references Id in roles table – Benson Okello Dec 29 '16 at 16:34
  • I mean you need to make sure it's passes from the form to the controller. You'll adding role ID through the form, right? So, execute `dd($request->role_id);` in the controller. It should show you role ID or `null`. – Alexey Mezenin Dec 29 '16 at 16:37
  • Thank you so much Alexey, however, Class 'App\Hash' not found is the error i received after trying to encrypt the password. – Benson Okello Dec 29 '16 at 16:42
  • Are you sure you're using `bcrypt()` global helper? Because it's a global helper which works from every part of application without adding any classes. It seems you're trying to use `Hash` facade instead. Please check this. – Alexey Mezenin Dec 29 '16 at 16:45
  • I corrected that error and it is no longer bringing it up. however,every time i try adding a new user, the role ID is what is displayed or returned on the page instead of the index.blade.php page. – Benson Okello Dec 29 '16 at 16:55
1

Hash::make will create a hashed password(Encrypted Password).

$password = Hash::make($request->password);
0

run following command to get the auth feature. please don't reinvent the wheel

php artisan make:auth

now all the scaffolding will be available. App\Http\Controllers\Auth\RegisterController bring the changes required. if you want roles to be added its preferred to use existing ACL packages.

you can even add adding new roles by editing the register controller, user model and creating new roles controller and model if you want.

anyways if you want to do it the way you are trying here is how you could

  • save the User Model

    User::create([
        'username' => $request->username,
        'email' => $request->email,
        'password' => bcrypt($request->password),
        'role_id' => $request->role_id
    ]);
    
  • check this common mistake - another thing to check is the database password column length, make sure the size more than 64.

  • check whether user model has .

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

aimme
  • 6,385
  • 7
  • 48
  • 65