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

codermans's avatar

LARAVEL 5.3 How to store empty DateTime input to Null value instead of 0000:00:00 00:00 value

i have a Flight Model which contain arriveDateTime, departDateTime, destination and remark fields. User are allowed to fill in either arriveDateTime or departDateTime field. For the empty input Datetime field, i want it to store as Null value in Database. But currently the empty input Datetime field is store as 0000:00:00 00:00 value in Database. What code should i modified?

0 likes
12 replies
bastman69's avatar

First you must update the field in the database to accept null values then you can set the field to null while saving.

eg. in your migration file

$table->string('email')->nullable();

and in your controller or model check if the users has completed a value or set to null

2 likes
codermans's avatar

hi i had update my database by setting the datetime field to $table->datetime('arriveDateTime')->default(null); $table->datetime('departDateTime ')->default(null);

But it still store 0000:00:00 00:00 value in Database.

How to set the field to null while saving??

bastman69's avatar

Please refer to the documentarion https://laravel.com/docs/5.4/migrations the way that the documentation mentions the null values for a field is as my previous.

propably you have a request object that passed from your view you may use a conditional

$request->arriveDateTime !== null ? mymodel->arriveDateTime = $request->arriveDateTime : mymodel->arriveDateTime=null;
ModestasV's avatar

@codermans Okay, you set it to default null but did you make it nullable? If so, please show us a screenshot of your database field with all its settings and your saving lines.

codermans's avatar

hi, bastman thanks for your help. But i still really don't know how to solve that. Can i send my code to your email so that you can have a look?

bastman69's avatar

you can post here.

copy paste your code inside 3 backticks

put your migration file, your view, your controller and your model.

codermans's avatar

Migration file:

public function up()
    {
        Schema::create('flightitems', function (Blueprint $table) {
            $table->increments('id');
            $table->datetime('arriveDateTime')->nullable()->default(null);
            $table->datetime('departDateTime')->nullable()->default(null);
            $table->string('destination');
            $table->text('remark')->nullable();
            $table->integer('customers_id')->unsigned();
            $table->foreign('customers_id')->references('id')->on('customers');
            $table->integer('flights_id')->unsigned();
            $table->foreign('flights_id')->references('id')->on('flights');
            $table->timestamps();
        });
    }

Controller for Store function

    public function store(Request $request)
    {
        $rules = array(
            'customer_name' => 'required',
            'arriveDateTime' => 'required_without:departDateTime',
            'departDateTime' => 'required_without:arriveDateTime',
        );
        $messages = array(
            'customer_name.required'=>'Name is required',
            'arriveDateTime.required_without'=>'Arrive DateTime required',
            'departDateTime.required_without'=>'Depart DateTime required',
        );
        
        $validator = Validator::make(Input::all(), $rules, $messages);
                    
        if($validator->fails()){
            return back()->withInput()->withErrors($validator);
        } 
        elseif ($validator->passes()){
            $flight = new Flight;

            if($flight->save()){
                $id = $flight->id;
                foreach($request->customer_name as $key => $v){
                    $data = array('flights_id' => $id,
                                'customers_id' => $v,
                                'arriveDateTime' => date('Y-m-d H:i:s', strtotime($request->arriveDateTime)),
                                'departDateTime' => date('Y-m-d H:i:s', strtotime($request->departDateTime)),
                                'destination' => $request->destination,
                                'remark' => $request->remark,
                    );
                Flightitem::insert($data);
                }
            }
        }
    }
Snapey's avatar

You are expecting this; date('Y-m-d H:i:s', strtotime($request->arriveDateTime)) to return null?

Tinker:

>>> date('Y-m-d H:i:s', strtotime(null))
=> "1970-01-01 00:00:00"
>>>
bastman69's avatar
public function up()
    {
        Schema::create('flightitems', function (Blueprint $table) {
            $table->increments('id');
            $table->datetime('arriveDateTime')->nullable();
            $table->datetime('departDateTime')->nullable();
            $table->string('destination');
            $table->text('remark')->nullable();
            $table->integer('customers_id')->unsigned();
            $table->foreign('customers_id')->references('id')->on('customers');
            $table->integer('flights_id')->unsigned();
            $table->foreign('flights_id')->references('id')->on('flights');
            $table->timestamps();
        });
    }
bastman69's avatar
Level 15
public function store(Request $request)
    {
        $rules = array(
            'customer_name' => 'required',
            'arriveDateTime' => 'required_without:departDateTime',
            'departDateTime' => 'required_without:arriveDateTime',
        );
        $messages = array(
            'customer_name.required'=>'Name is required',
            'arriveDateTime.required_without'=>'Arrive DateTime required',
            'departDateTime.required_without'=>'Depart DateTime required',
        );
        
        $validator = Validator::make(Input::all(), $rules, $messages);
                    
        if($validator->fails()){
            return back()->withInput()->withErrors($validator);
        } 
        elseif ($validator->passes()){
            $flight = new Flight;

            if($flight->save()){
                $id = $flight->id;
                foreach($request->customer_name as $key => $v){
                    $data = array('flights_id' => $id,
                                'customers_id' => $v,
                                'arriveDateTime' => $request->arriveDateTime !== null ? date('Y-m-d H:i:s', strtotime($request->arriveDateTime)) : null,
                                'departDateTime' => $request->departDateTime !== null ? date('Y-m-d H:i:s', strtotime($request->departDateTime)) : null,
                                'destination' => $request->destination,
                                'remark' => $request->remark,
                    );
                Flightitem::insert($data);
                }
            }
        }
    }

you must refresh your migrations first. And better use Carbon for this

date('Y-m-d H:i:s', strtotime($request->arriveDateTime))
codermans's avatar

@bastman69, the code worked! thanks so much by the way, what does it mean by:

And better use Carbon for this date('Y-m-d H:i:s', strtotime($request->arriveDateTime))

i never try Carbon before

Please or to participate in this conversation.