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

Fennek's avatar

Problem with storing date from form

So I'm trying to store a birth date in the database as an additional field with user registration (auth).

Form:

  <div class="form-group{{ $errors->has('birth') ? ' has-error' : '' }}">
            <label class="col-md-4 control-label">Date of Birth*</label>

            <div class="col-md-6">
                <input type="text" placeholder="dd/mm/YYYY" class="form-control" name="birth" value="{{ old('birth') }}">

                @if ($errors->has('birth'))
                    <span class="help-block">
                                    <strong>{{ $errors->first('birth') }}</strong>
                                </span>
                @endif
            </div>
</div>

Controller:

protected function create(array $data)
    {
        return User::create([
            ....other fields here
            'birth' => $data['birth'],
            .....other fields here
        ]);
    }

I'm assuming I have to somehow reformat the input data but I'm not really sure on how to do this and especially on where to put it in laravel. Anyone who could help me out?

0 likes
3 replies
martinbean's avatar

@Fennek You’ll need to re-format the date from DD/MM/YYYY to YYYY-MM-DD before passing to your model:

$data = $request->all();
$data['birth'] = Carbon::createFromFormat('DD/MM/YYYY', $data['birth'])->toDateString();

return User::create($data);
Fennek's avatar

@martinbean Thank you for your response. Where would this piece of code go? I'm rather new to this. Does it replace the old return? I tried it out but I'm getting an undefined variable error on $request

MaverickChan's avatar
$data->birth = $request->input('birth');

or

'birth' => $request->input('birth'),

assuming you are using a form to input the birthday. maybe you should try some date picker js , save a lot of time

Please or to participate in this conversation.