Laravel - Integrity constraint violation: 1062 Duplicate entry

Bullgod

i have a problem when i try to create a new user in my database, if I add the users in phpmyadmin i have no problems, but when i try create users in my laravel web, the users has created with this error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '15.236.964-5' for key 'PRIMARY' I dont have idea which is the problem because in the database does not exist the same primary key. The table with problems is:

CREATE TABLE IF NOT EXISTS `users` (
`rut` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` char(60) COLLATE utf8_unicode_ci NOT NULL,
  `edad` int(11) NOT NULL,
  `pais` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `comuna` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `sector` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tipo` enum('profesor','alumno','administrador_parrilla','administrador_sistema','externo') COLLATE utf8_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `users`
  ADD PRIMARY KEY (`rut`),
  ADD UNIQUE KEY `usuario_rut_unique` (`rut`),
  ADD UNIQUE KEY `usuario_email_unique` (`email`);

My controller:

class UsuarioController extends Controller
{
	public function index(){
        $users = User::All();
        return view('usuario.index', compact('users'));
    }

    public function create(){
    	return view('usuario.create');
    }
    public function store(Request $request) {
         User::create([
             'name' => $request['name'],
             'rut' => $request['rut'],
             'email' => $request['email'],
             'password' => bcrypt($request['password']),             
             'edad' => $request['edad'],
             'pais' => $request['pais'],
             'comuna' => $request['comuna'],
         ]);
        User::create($request->all());
        return redirect('/usuario')->with('message', 'store');
    }

    public function edit($rut){
        $user = User::find($rut);
        return view ('usuario.edit',['user'=>$user]);
    }

    public function update($id, Request $request){
        $user = User::find($id);
        $user -> fill($request->all());
        $user -> save();

        Session::flash('message', 'Usuario Editado Correctamente');
        return Redirect ('/usuario');
    }
}

My Model:

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = "users";
    protected $fillable = [
        'rut', 'name', 'email', 'password', 'edad', 'pais', 'comuna', 'sector', 'tipo',
    ];
/**
    protected $primaryKey = 'rut';

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

I can't change the primary key adding auto_increment because i use the "Rut" as a primary key who is the code of identification to people in Chile.

Bullgod

The problem was a line of code in the controller. The solution was simply to comment this line:

User::create($request->all());

Thanks for the answers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

Laravel - Integrity contraint violation: 1062 Duplicate entry

From Dev

"Integrity constraint violation: 1062 Duplicate entry" - but no duplicate rows

From Dev

Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

From Dev

Magento - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

SQLState 23000 - Integrity constraint violation 1062 Duplicate Entry

From Dev

Integrity constraint violation: 1062 Duplicate field

From Dev

Save model in Yii twice - getting "Integrity constraint violation: 1062 Duplicate entry"

From Dev

Duplicating a Magento site causes SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

Doctrine & Symfony2 - Integrity constraint violation: 1062 Duplicate entry 'x-y' for key 'PRIMARY

From Dev

File Upload - Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY

From Dev

Magento 2 - Integrity constraint violation: 1062 Duplicate entry - Error in setup:upgrade after upgrade from community to enterprise

From Dev

Cakephp 2.5.4: Database Error Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY', query was: INSERT INTO `log_visitor`

From Dev

Doctrine & Symfony2 - Integrity constraint violation: 1062 Duplicate entry 'x-y' for key 'PRIMARY

From Dev

How Can i solved this error:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '30' for key 'PRIMARY'

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '7775-683756' for key 'camera_from_to_unique'

From Dev

Yii - CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5375' for key 'PRIMARY'

From Dev

Laravel 5: Integrity constraint violation: 1062 - Many to Many

From Dev

PDO "Integrity constraint violation - Duplicate entry", but no duplicates in table

From Dev

PDO "Integrity constraint violation - Duplicate entry", but no duplicates in table

From Dev

Laravel : Integrity constraint violation

From Dev

Integrity constraint violation in laravel

From Dev

Integrity constraint violation in laravel

From Dev

laravel Integrity constraint violation

From Dev

Integrity constraint violation: 1452 laravel

From Dev

Laravel validator - Integrity constraint violation

From Dev

Integrity constraint violation: 1452 laravel

Related Related

  1. 1

    Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  2. 2

    Laravel SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  3. 3

    Laravel - Integrity contraint violation: 1062 Duplicate entry

  4. 4

    "Integrity constraint violation: 1062 Duplicate entry" - but no duplicate rows

  5. 5

    Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

  6. 6

    Magento - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  7. 7

    SQLState 23000 - Integrity constraint violation 1062 Duplicate Entry

  8. 8

    Integrity constraint violation: 1062 Duplicate field

  9. 9

    Save model in Yii twice - getting "Integrity constraint violation: 1062 Duplicate entry"

  10. 10

    Duplicating a Magento site causes SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  11. 11

    Doctrine & Symfony2 - Integrity constraint violation: 1062 Duplicate entry 'x-y' for key 'PRIMARY

  12. 12

    File Upload - Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY

  13. 13

    Magento 2 - Integrity constraint violation: 1062 Duplicate entry - Error in setup:upgrade after upgrade from community to enterprise

  14. 14

    Cakephp 2.5.4: Database Error Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

  15. 15

    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY', query was: INSERT INTO `log_visitor`

  16. 16

    Doctrine & Symfony2 - Integrity constraint violation: 1062 Duplicate entry 'x-y' for key 'PRIMARY

  17. 17

    How Can i solved this error:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '30' for key 'PRIMARY'

  18. 18

    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '7775-683756' for key 'camera_from_to_unique'

  19. 19

    Yii - CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5375' for key 'PRIMARY'

  20. 20

    Laravel 5: Integrity constraint violation: 1062 - Many to Many

  21. 21

    PDO "Integrity constraint violation - Duplicate entry", but no duplicates in table

  22. 22

    PDO "Integrity constraint violation - Duplicate entry", but no duplicates in table

  23. 23

    Laravel : Integrity constraint violation

  24. 24

    Integrity constraint violation in laravel

  25. 25

    Integrity constraint violation in laravel

  26. 26

    laravel Integrity constraint violation

  27. 27

    Integrity constraint violation: 1452 laravel

  28. 28

    Laravel validator - Integrity constraint violation

  29. 29

    Integrity constraint violation: 1452 laravel

HotTag

Archive