I have a table where I have all the employee details. I want to make employee_id and only month and year from date_of_salary as unique on the table. Only one row should be present for that particular employee ID and month/year. The date_of_salary is a column with date datatype.
I tried using the following but this works for the employee id and complete date. It takes multiple entries for same month.
$table->unique(['employee_id', 'date_of_salary']);
This is my complete up function for the migration
public function up()
{
Schema::create('employee_salary_details', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('employee_id')->required();
$table->date('date_of_salary')->required();
$table->foreign('employee_id')
->references('id')
->on('employees')
->onDelete('restrict');
$table->unique(['employee_id', 'date_of_salary']);
$table->timestamps();
});
}
Each row should be unique for an employee and salary month/year. Because the salary can happen only once a month but on any date.