I am working on employee management system project in laravel in which the requirement is to store the employee information with as much column as the user wants. I cannot remain table columns fixed. Admin has the functionality to add employee information with its own created columns like Name: Khizer, Age: 20 etc.
So I managed this scenerio in the following way:
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
**Employee Table:**
ID created_at updated_at
12 2020-9-21 2020-9-21
23 2020-9-21 2020-9-21
CREATE TABLE `columnvalues` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`employee_id` int(11) DEFAULT NULL,
`employee_column_id` int(11) DEFAULT NULL,
`column_value` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_column` FOREIGN KEY (`employee_column_id`) REFERENCES `employee_columns` (`id`),
CONSTRAINT `fk_emp` FOREIGN KEY (`employee_id`) REFERENCES `employee` (`id`)
)
**ColumnValues**
ID EmployeeId Column_Id Value
1 12 20 Khizer
2 12 21 20
CREATE TABLE `employee_columns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`columnname` varchar(200) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
**Columns Table**
ID Column_Name
20 Name
21 Age
I created three tables to store as much value as needed. 1st employee, 2nd columns and third is the associative entity between employee and columns. Is this better solution or there is other option to handle this scenrio?