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

yhassoun's avatar

Date stores as null in mysql

I am storing date in this format: yyyy-mm-dd I also tried with yyyy-mm-dd hh:mm:ss using date and strtotime functions in a livewire component. I can read the variable with no issues if I dd with this format, but once I write it to mysql db it is stored as null: date('Y-m-d', strtotime($this->date)) I tried field type in db to be DATETIME as well as TIMESTAMP, both with no luck. What am I doing wrong?

0 likes
8 replies
yhassoun's avatar

@SilenceBringer Livewire component:

class JobAdd extends Component {
    public close_date; //other non related vars
	
	public function submit(){
        $formated_deadline = date('Y-m-d', strtotime($this->close_date));
        $jobPost = ([
            'close_date'  => $formated_deadline, 
           //Other non related fields 
]);
           Job::create($jobPost);
		}
}

Blade file:

<input class="tailwind classes" type="date" wire:model="close_date">
  <button class="tailwind classes" wire:click="submit()">
     Submit
   </button>           

Model Job:

protected $fillable = [
       'number', 'close_date', //some other fields
    ];

I am using mysql with laragon. all the fields are written properly but this one goes as null. I tried both 'datetime' field time in DB and 'timestamp', same result NULL

yhassoun's avatar

@SilenceBringer Storing happens here: Job::create($jobPost); Storing is fine and once I dd I get the right format for example: 2021-11-22 it is only on DB that it reflects as null. I am still learning Laravel, the documentation can be a little challenging for me. Thanks for the link though, I will go through it

Jonjie's avatar

Try to change the data type to varchar instead of date types. BUT it is not recommended.

yhassoun's avatar
yhassoun
OP
Best Answer
Level 1

The problem was in the format I entered, it was not correct. It should be: formated_deadline = date('Y-m-d H:i:s', strtotime($this->close_date)) That solved it :)

Please or to participate in this conversation.