Update in laravel with primary key other than id

3

I am starting to work with laravel, but the database was already created and in none of the tables the primary key is named id .

The roles table has its primary key id_role when trying to do the update it tells me the following error:

  

SQLSTATE [HY000]: General error: 20018 Invalid column name 'id'. [20018] (severity 16) [update [roles] set [COD_ROLE] = '1111',   [DES_ROLE] = 'Sysadmin2', [FDM_ROLE] = '2018-07-31' where [id] is   null] (SQL: update [roles] set [COD_ROLE] = 1111, [DES_ROLE] =   Sysadmin2, [FDM_ROLE] = 2018-07-31 where [id] is null)

In my model role I have already redefined the primary key:

class role extends Model
{
     /*
     Modificar convenciones de Laravel:
     */   
    //Para indicar que la tabla del modelo no usará los campos created_at y updated_at
    public $timestamps = false;
    //Indicar que la clave de la tabla no es id sino id_role
    protected $primary_key = 'ID_ROLE';
}

Having this in the controller:

public function update(Request $request)
    {
        //
        $rol = role::where("id_role", '=', $request['ID_ROLE'])->first();

        $rol->COD_ROLE = $request['COD_ROLE'];
        $rol->DES_ROLE = $request['DES_ROLE'];
        $rol->OBS_ROLE = $request['OBS_ROLE'];
        $rol->FDM_ROLE = Date('Y-m-d');
        $rol->EST_ROLE = $request['EST_ROLE'];
        $rol->save();
    }

Can someone help me and tell me what I'm doing wrong, why does not it work?

    
asked by Virginia 31.07.2018 в 18:25
source

1 answer

-1

In the model add this:

public function getKeyName(){
    return "id_role";
}
    
answered by 31.07.2018 / 18:36
source