Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Date value is stored as 0000-00-00 in DB

I am trying to store values from a Datepicker in my MySQL DB with php Laravel.

The request works AFAIK, since when I dd the request the date is displayed, but when looking into the database I only see "0000-00-00" and not the date, so I guess something is lost with my controller but I don’t know what.

Here is my Controller method:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public function storePost(Request $request) {


        $this->validate($request, [
            'titel' => 'required|max:255',
            'standort' => 'required|max:255',
            'kontakt' => 'required|email|max:255',
            'startdatum' => 'required|date',
            'enddatum' => 'required|date',
            'beschreibung' => 'required|',
        ]);

        $request->user()->posts()->create([
            'titel' => $request->titel,
            'standort' => $request->standort,
            'kontakt' => $request->kontakt,
            'startdatum' => $request->startdatum,
            'enddatum' => $request->enddatum,
            'beschreibung' => $request->beschreibung,
        ]);

        return redirect()->route('home');
    }

I already tried it with this version after seeing it on stack overflow, but it didn’t work either:

public function storePost(Request $request) {

        $this->validate($request, [
            'titel' => 'required|max:255',
            'standort' => 'required|max:255',
            'kontakt' => 'required|email|max:255',
            'startdatum' => 'required|date',
            'enddatum' => 'required|date',
            'beschreibung' => 'required',
        ]);

        $request->user()->posts()->create([
            'titel' => $request->titel,
            'standort' => $request->standort,
            'kontakt' => $request->kontakt,
            'startdatum' => Carbon::parse($request->startdatum),
            'enddatum' => Carbon::parse($request->enddatum),
            'beschreibung' => $request->beschreibung,
        ]);

        return redirect()->route('home');
    }

I am not sure if it is relevant, but here is the snippet of the html code where the date is coming from:

            <div class="flex flex-wrap -mx-3 mb-6">
                <div class="w-full px-3">
                    <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2 text-base" for="startdatum">
                        Startdatum
                    </label>
                    <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded
                    py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white
                    focus:border-gray-500 @error('startdatum') border-red-500 @enderror" id="startdatum" name="startdatum" type="date" placeholder="Startdatum">
                    @error('startdatum')
                    <div class="text-red-500 mt-2 text-sm">
                        {{ $message }}
                    </div>
                    @enderror
                </div>
            </div>


            <div class="flex flex-wrap -mx-3 mb-6">
                <div class="w-full px-3">
                    <label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2 text-base" for="enddatum">
                        Enddatum
                    </label>
                    <input class="appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded
                    py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white
                    focus:border-gray-500 @error('titel') border-red-500 @enderror" id="enddatum" name="enddatum" type="date" placeholder="Enddatum">
                    @error('enddatum')
                    <div class="text-red-500 mt-2 text-sm">
                        {{ $message }}
                    </div>
                    @enderror
                </div>
            </div>

>Solution :

in your Post Model, make sure you have $fillable property with correct properties:

class Post extends Model
{
 protected $fillable = ['standort','titel','kontakt','startdatum','enddatum','beschreibung'];
....
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading