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

pierre4854's avatar

Unable ton insert date on Database

Hello,

I get a date from a form and I want to insert it on Database. So, this is my controller for format my date:

            $request->date_of_birth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->format('Y-m-d');

When I make a dd($request->date_of_birth), I'm getting this : "2016-03-08" So everything is ok for me but when I want to insert on database, I got a 0000-00-00 And i can't understand why. I've already searched on forums some solutions like add ->toDateTimeString() but it's not working. Anyone has an idea please ?

0 likes
14 replies
jlrdw's avatar

In your database make sure you have a date field.

pierre4854's avatar

Here is my code and yes, when I fill an other field, it's inserted well on the DB :

    public function update(Request $request, $userId)
    {
        $user = User::findOrFail($userId);
        if ($user){
            if (Auth::check() && $user == Auth::user()){
            $this->validate($request, [
                'nickname'      => "required|unique:users,nickname,{$user->id}|min:2",
                'last_name'     => 'min:2',
                'first_name'    => 'min:2',
                'email'         => 'required|email',
                'date_of_birth' => 'date_format:d/m/Y',
                'country'       => 'min:2',
                'city'          => 'min:2',
                'avatar'        => 'image',
            ]);
            //Format the date for the database
            $request->date_of_birth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->format('Y-m-d');


            // User update
            $user->update($request->only('nickname', 'email', 'last_name', 'first_name', 'gender', 'date_of_birth', 'country', 'city', 'games', 'avatar'));
            return back()->with('success', "Votre profile a bien été modifié !");
            }else{
                return back()->with('error', "Vous n'êtes pas autorisé à modifier ce profile !");
            }
        }else{
            return redirect('/')->with('error', 'Profile inexisant');
        }
    }

I can't see a solution on your link because to format the date, we need to create here before, like I already do on my controller

jlrdw's avatar

Did you see my edit, look over the other post.

pierre4854's avatar

I've already saw your other post and and I answer your on editing my question, but I give it to you back :

I can't see a solution on your link because to format the date, we need to create here before, like I already do on my controller

jlrdw's avatar

Try testing without carbon

date_format($request->date_of_birth, 'Y-m-d');

If that works, then there is a formatting issue with carbon.

pierre4854's avatar

It will return an error because the first parameter is not a DateTime, it's just a string from the field

thomaskim's avatar
Level 41

You cannot do this:

$request->date_of_birth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->format('Y-m-d');

There is no magic setter methods in the Request class. Try using the replace method instead.

$request->replace(['date_of_birth' => Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->format('Y-m-d')]);
pierre4854's avatar

@thomaskim Whoops, I come back to say that it replace all my requests parameters and it keep only the date. How I can solve it ? Because I want other parameters too ^^

thomaskim's avatar

@ARAGORND7 Sorry about that. :) Use the merge method instead of replace then.

$request->merge(['date_of_birth' => Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->format('Y-m-d')]);

Please or to participate in this conversation.