Update with Laravel does not take the model id

0

I'm trying to perform a simple function to change the password of the user logged in using the code:

$user = User::find(\Auth::user()->idusuario);
$user->password = bcrypt($request['novasenha']);
$user->save();

But, it does not work. I noticed that the executed query does not take the id of the user:

update USUARIOS set password = ? where IDUSUARIO is null

I noticed that this problem of " where IDUSUARIO is null " is also repeated in other situations. How should I proceed with this? Why does not he get the id being passed?

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

    protected $table = "USUARIOS";
    protected $primaryKey = 'IDUSUARIO';
    public $timestamps = false;

    /**
     * The database table used by the model.
     *
     * @var string
     */

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'idusuario', 'nomerazao', 'email', 'cpfcnpj', 'telefone', 'idperfil', 'situacao', 'password'
    ];

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

    public function getAuthIdentifier()
    {
        // TODO: Implement getAuthIdentifier() method.
        return $this->attributes['idusuario'];
    }

    public function regras(){
        return $this->belongsToMany('App\Models\RegrasModel', 'REGRASUSUARIOS', 'IDUSUARIO', 'IDREGRA');
    }

}
    
asked by anonymous 06.09.2016 / 14:11

3 answers

1

This is not an error. In fact, maybe the column name in the table or whatever else is wrong.

The problem is being caused because Auth::user()->idusuario is returning NULL .

For example, if you try to find , the query looks like this:

 DB::enableQueryLog();
 DB::table('usuarios')->find(null);
 DB::getQueryLog ();

Result:

  

select * from usuarios where id is null limit 1

Another thing: Whenever you use a framework try to use its conventions. Be it for database, or for anything else (name of methods and the like).

The widely used Laravel pattern for Primary Keys is simply id .

You can, of course, change this, but remember to do so in a way that does not deviate too much from the standards of the Framework.

I noticed that you used the names definition of some attributes in UpperCase, and then so much access them with LowerCase. This is not a good idea!

Always keep a pattern for writing your codes!

It is also important to remember that Laravel will use the __get method to access the value of the attributes, which in turn are stored in the Model::$attributes property.

A test you can do to detect your error is to debug the values that are being stored in the model, like this:

 dd(Auth::user()->getAttributes());

You will notice that Laravel will not do the magic conversion of names in UpperCase to LowerCase, for you to access them.

In your case, you do not even have to find , since Auth::user() returns the Model instance used for the authentication component.

Here's how you can do it:

 Auth::user()->update(['password' => bcrypt('senha')])
    
06.09.2016 / 15:21
1

When doing this function, you already load the user variable with the authenticated user.

$user = \Auth::user();
$user->password = bcrypt($request['novasenha']);
$user->save();
    
06.09.2016 / 16:45
0

I make the words from @Wallace Maxters (refer to a user has been removed?) mine, but it seems to me that the main reason for the problem is the value that you set the "primaryKey" property within your template. It is set to uppercase, like this:

protected $primaryKey = 'IDUSUARIO';

What results in a query with the column letters also in upper case, like this:

update USUARIOS set password = ? where IDUSUARIO is null

Try changing to lowercase (or change to the exact value of your database), like this:

protected $primaryKey = 'idusuario';

However, if your authenti- cation system is working, try using the Auth facade to get the logged in user, like this:

$loggedInUser = \Auth::user();
$loggedInUser->setAttribute('password', '...');
$loggedInUser->save();

A simple test that you may be performing is, the Auth facade has a method to get the id of the authenticated user, try to use find with this value, like this:

$loggedInUserId = \Auth::id();

$loggedInUser = User::find($loggedInUserId);
$loggedInUser->setAttribute('password', '...');
$loggedInUser->save();

I also believe that it is not necessary to add the column "idusuario" in the "fillable" property of your model.

    
07.09.2016 / 18:23