1

I am developing my own login and I have the following code

public function login(Request $request ) {
         $email = \Request::input('email');  
        $password = \Request::input('password');              
        if (Auth::attempt(['email' => $email, 'password' => $password]))  
        {  
            //echo "success";  
            return redirect('home');  
        }  
        else {  
            return "fail";  
        }  
    }

And the CreateUserRequest

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateUserRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'nif' => 'required | max:9 ',
            'name' => 'required | max:255',
            'email' => 'required',
            'cognoms' => 'required | max:255',
            'birthday' => 'required',
            'password' => 'required | confirmed',
            'password_confirmation' => 'required',
            'municipios' => 'required | Integer|Min:1',
            'presentacion' => 'required',
            'file' => 'required'

        ];
    }

}

My register controller

public function registro(CreateUserRequest $request){ 

        $usuario = new User();
        $usuario->nif = \Request::input('nif');     
        $usuario->name = \Request::input('name');
        $usuario->cognoms = \Request::input('cognoms');
        $usuario->birthday = \Request::input('birthday');
        $usuario->email = \Request::input('email');
        $usuario->password= \Request::input('password');
        /**Foto del usuario**/ 
        $file = \Request::file('file');
        $fileName = $file->getClientOriginalName();
        $file->move(public_path().'/uploads/', $fileName);
        $usuario->file = 'uploads/'.$fileName.'';
        $usuario->save();
        /**Asignamos el rol a la tabla intermedia***/
        $user = User::find($usuario->id);
        $user->roles()->attach(1);
        return redirect('/');
    }

The model

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['nif','name','cognoms','email', 'password','idempresa','id_poblacion','id_online',];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

     public function empresa()
    {
        return $this->belongsTo('Empresa');
    }

     public function municipio()
    {
        return $this->belongsTo('App\Ciudad','id_poblacion');
    }

     public function roles()
    {
        return $this->belongsToMany('App\Rol')->withPivot('user_id','rol_id');
    }

    public function mensajes()
    {
        return $this->belongsToMany('App\User')->withPivot('id_emisor','id_receptor');
    }

    public function subastas(){
        return $this->hasMany('App\Subasta','id_creador','id');
    } 

    public function pujas(){
        return $this->hasMany('App\Puja','id_subasta','id');
    } 
}

When I put the password , the laravel create the user andthe field password in database is white.

9
  • pls may I see the code for your user model? Commented May 26, 2015 at 9:32
  • Shouldn't it if (Auth::attempt(array('email' => $email, 'password' => $password))) ? Commented May 26, 2015 at 9:34
  • please can you show us the error message you got? Commented May 26, 2015 at 9:43
  • please can you change this $usuario->password = \Hash::make('password'); to $usuario->password = \Request::input('password'); and register a new user and try to login once more. This is because public function setPasswordAttribute($password) { $this->password = \Hash::make($password); } in the model will automatically hash the password before storing it in db Commented May 26, 2015 at 10:05
  • Hello @Digitlimit , I make this changes and the field password in database is white , do you have an example about customize login and register to see how could I doing? Commented May 26, 2015 at 10:12

1 Answer 1

1

Please try this:

public function registro(CreateUserRequest $request){

            $file = $request->file('file');
            $fileName = $file->getClientOriginalName();
            $file->move(public_path().'/uploads/', $fileName);


            $user = User::create([
                'nif' => $request->input('nif'),
                'name' => $request->input('name'),
                'cognoms' => $request->input('cognoms'),
                'birthday' => $request->input('birthday'),
                'email' => $request->input('email'),
                'password' => \Hash::make($request->input('password')),
                'file' => 'uploads/'.$fileName.'',
            ]);


            /**Asignamos el rol a la tabla intermedia***/
            $user = User::find($user->id);
            $user->roles()->attach(1);
            return redirect('/');


   }
Sign up to request clarification or add additional context in comments.

11 Comments

The password field send white equal :( , I review all of my code another time.
what datatype is your password field. check your user migration table
Is the same $table->string('password', 60) , I don't change this. I supose the order in the model it's one of the posibilities?
I delete the public function password in the model and I sent correctly the password into database I think that in my opinion I have a problem with the setAttributePassword I try your option and I have the same problem before I mentioned.
so lets go back to $usuario->password= Hash::make(\Request::input('password')); and also remove the setPasswordAttribute method from the model. Please also dd(Hash::make(\Request::input('password'))) in the registro() to see if it produces a hashed pasword
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.