Change column name ID - Auth Laravel 5.5

0

I'm doing a login with laravel, I had a table already created. The problem is that in my table the id has another name and when trying to login it shows me this error.

"Undefined index: id"

This is User.php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable {
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */

protected $primaryKey = 'IdPersonal';

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

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

I understand that I have to change id by the name of the column that I have in my table but I do not know how to do it.

    
asked by cristians bregante 05.10.2017 в 23:14
source

2 answers

1

If this field is the primary key of the model, then you simply change that value in the model.

Assuming the field is named custom_id , add the following in the User.php model:

/**
 * The primary key for the model.
 *
 * @var string
 */
protected $primaryKey = 'custom_id';
    
answered by 05.10.2017 в 23:21
0

With the little information you give it is difficult to know well what you need

1. DB:

  • The table does not exist.

      

    Solution: Migrate the table. if you do not have migrations, create the migrations first

  • The PK does not exist or is not assigned to a column.

      

    Solution: Edit the migration to create the column or modify the one that exists as PK, and then migrate the DB again

  • The PK has a different name than expected.

      

    Solution: Edit the migration to change the name of the station, and then migrate the DB again

2. Views:

  • The variables in the form do not match the controller.
      

    Solution: Edit the name of the form's input to match what the controller expects to receive

3. Code: this part is a bit more ambiguous because it depends on whether you are using laravel's login and modifying it or you created your own login from 0:

  • Editing laravel's login:

    • the error comes from the code used in the authentication.

        

      Solution 1 : check the names used to represent the id in the folder's controllers

           
          

      / app / Http / Controllers / Auth /

        
           

      and edit them to match your preferences

           

      Solution 2: Review the User.php model you are using and verify that the variable defined in the model is the same as the variable in the database. (according to the information you gave, this should not be the problem)

  • creating your own custom login from scratch.
    • code error.
        

      Solution: The error can come from any of your files used for this purpose, check everything seen in the other solutions in your own files.

  • PS: try to show more information relevant to these cases

        
    answered by 15.10.2017 в 18:41