不需要日期字段,但没有它,表单将不会提交

I am making a create post form with the date field not required and i made a mutator to make the date NULL in database when not entered in form.

Form in create.blade.php

<div class="form-group {{ $errors->has('published_at') ? 'has-error' : '' }}">
    {!! Form::label('published_at', 'Publish date') !!}
    {!! Form::text('published_at', null, ['class' => 'form-control', 'placeholder' => 'Y-m-d H:i:s']) !!}
    @if($errors->has('published_at'))
        <span class="help-block">{{ $errors->first('published_at') }}</span>
    @endif
</div>

Mutator in Post.php:

public function setPublishedAtAttribute($value)

{
    $this->attributes['published_at'] = $value ?: NULL;
}

Store function in controller:

$this->validate($request, [

    'title' => 'required',
    'slug' => 'required|unique:posts',
    'body' => 'required',
    'published_at' => 'date_format:Y-m-d H:i:s',
    'category_id' => 'required'

]);

I expect the date to be NULL in database when not entered in form , but i get a validating error in form "The published at does not match the format Y-m-d H:i:s." and it won't submit

You can do the following

      'published_at' => 'nullable|date_format:Y-m-d H:i:s',

and in you migration file you can set the attribute as nullable

$table->timestamp('published_at')->nullable();

So if there isn't a value ,will be nullable

Just make validation published_at=>'sometimes|date_format:Y-m-d H:i:s'