it fails even providing valide date?
Oct 3, 2016
11
Level 2
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
Please or to participate in this conversation.