Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

icodebuster's avatar

Date uniqueness validation fails

I am having a date field in DB, the date is a Carbon instance. Now I need to have uniqueness validation on this date column.

My Migration

public function up()
  {
  Schema::create('holidays', function (Blueprint $table) {
  $table->increments('id');
  $table->date('date')->unique()->nullable();
  $table->string('day', 16)->nullable();
  $table->string('description', 100)->nullable();
  $table->timestamps();
  });
  }

My Model

protected $dates = ['date'];

public function setDateAttribute($date)
  {
  if ($date) {
  $this->attributes['date'] = Carbon::parse($date);
  } else {
  $date = null;
  }
  }

My FormRequest

return [
  'date' => 'unique:holidays,date',
  'description' => 'required',
  ];

The issue is that the validation is getting failed. How do I make date unique. As an alternative, I can save it as a string where the uniqueness is working but I need to sort is on basis of date.

Update: The date format that is saved in DB is d-m-Y

0 likes
11 replies
icodebuster's avatar

yes, the validation fails and I get error at database level. Duplicate Entry

a23mer's avatar

Try replacing comma with '|' :


return [
      'date' => 'unique:holidays|date',
      'description' => 'required',
  ];
icodebuster's avatar

the date is the column name in holidays table. Refer Laravel Docs

@a23mer still I tried but its not working.

Update: The error is as follows.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2016-10-05' for key 'holidays_date_unique' 
(SQL: insert into `holidays` (`day`, `date`, `description`, `updated_at`, `created_at`) 
values (, 2016-10-05 00:00:00, Testing, 2016-10-04 12:50:24, 2016-10-04 12:50:24))
a23mer's avatar

My mistake, I thought "date" was for validating the data type. I can't figure out what's wrong. I usually use a Validator::make with the input fields and the rules as parameters, then redirect it back with errors if the validator fails.

icodebuster's avatar

Even if I use the validator the same issue occurs, for time being I have changed the date type to string and the uniqueness is working, I wanted to keep the date as a Carbon instance.

pmall's avatar

Strange. Dates are mutated so a carbon instance is transformed into a suitable value when saving to the database. Maybe the mutator are not used for uniqueness validation? Did you put the 'date' field into the $dates array of the model?

icodebuster's avatar

@pmall yes, Should I use the getDateAttribute method. I had used it since the date can be nullable.

public function setDateAttribute($date)
  {
  if ($date) {
  $this->attributes['date'] = Carbon::parse($date);
  } else {
  $date = null;
  }
  }

pmall's avatar

Why dont you just put it in the $dates array?

icodebuster's avatar

@pmall I tried is as per your suggestion still the same issue.

My Model

protected $dates = ['date'];

My controller

public function store(HolidayFormRequest $request)
    {
        $newHoliday = new Holiday();
        $newHoliday->day = $request->input('day') == "" ? null : $request->input('day');
        $newHoliday->date = $request->input('date') == "" ? null : Carbon::parse($request->input('date'));
        $newHoliday->description = $request->input('description');
        $newHoliday->save();
        Flash::success(trans('webresponse.insertSuccess'));
        return redirect()->route('holiday.index');
    }

My Form Request

public function rules()
{
    return [
        'date'  => 'unique:holidays',date,     // even tried 'unique:holidays',
        'description' => 'required',
    ];
}

Please or to participate in this conversation.