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

sanjayacloud's avatar

How to insert date into table in laravel

Hello,

I am trying to insert course date what I choose from my form to table. But there is no data inserted in my course_date column. My controller is like below.

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|max:255',
            'description' => 'required',
            ''
        ]);

        if ($validator->fails()) {
            return redirect('courses/create')
                ->withErrors($validator)
                ->withInput();
        }

        $image= $request->file('feature_image');
        $image_path = Image::make($image);
        $set_img_path = time().$image->getClientOriginalName();
        $image_path->save( public_path().'/images/course/'.$set_img_path);

        $data = new Courses;

        $now = date('Y-m-d H:i'); //Fomat Date and time
        $now = $request->couese_date;
        
        $data['title'] = $request->title;
        $data['description'] = $request->description;
        $data['course_date'] = $now;
        $data['feature_image'] = $set_img_path;
        $data->save();
        return back()->with('status', 'Course Successfully Added');

    }

Here is my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Courses extends Model
{
    protected $fillable = ['title', 'description', 'feature_image', 'course_date'];
}

Any one can help me to fix this?

0 likes
5 replies
manelgavalda's avatar

Looks like you have a typo here:

...
        $data = new Courses;

        $now = date('Y-m-d H:i'); //Fomat Date and time //you are overwriting this variable below
        $now = $request->couese_date; //should be course_date
        
        $data['title'] = $request->title;
...
1 like
Cronix's avatar
$now = $request->couese_date;

That's what's being inserted. Is that spelled correctly (course_date?)? If not, it will be null.

Also, remove that empty string from the end of your validation rules.

Cronix's avatar

Why would your call it couese_date there, when you call it course_date in your fillable array as well as the db table that you're saving? It should be consistent everywhere.

Next question is what is the actual value you're trying to save?

sanjayacloud's avatar

@CRONIX - Actually, I am trying to insert course start date. Any way, I have sorted it now. Thank you guys for your support.

Please or to participate in this conversation.