Differences in fields names in user table

Published 4 months ago by [email protected]

I have to use to my project existing table 'users'. I cannot change fields names. How can I config Laravel to use table like this.

CREATE TABLE `users` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `login` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `isadmin` bit(1) DEFAULT NULL,
  `department` varchar(255) DEFAULT NULL,
  `warehouse` varchar(250) CHARACTER SET utf8 NOT NULL,
  `alias` varchar(100) CHARACTER SET utf8 NOT NULL,
  `role` varchar(255) DEFAULT NULL,
  `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`userid`,`login`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=latin2;
Best Answer (As Selected By [email protected])
MoHoKh67

Add this one to your User model

protected $primaryKey = 'userid';

ElectroWeb

May you please elaborate more? What is the problem you are experiencing?

pio.skro@gmail.com

(2/2) QueryException

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from users where id = 1 limit 1)

MoHoKh67

I don't see any problem with your table but you can always create a view table and rename fields and table to the one you like or Laravel requires.

MoHoKh67

Add this one to your User model

protected $primaryKey = 'userid';

pio.skro@gmail.com

Thank You. It works.

Please sign in or create an account to participate in this conversation.