user13971133
user13971133

Reputation:

why the date information cannot be saved into mySQL database?

My code is in PHP Laravel. And I tried to create a table in font-end which allow user to select my_date information from the drop-down calendar. Then, in back-end, I tied to save the table information into database via migration. But why the date information cannot be saved into mySQL database?

enter image description here

In config/database.php I already have

'strict' => false,

In index.blade.php file

<input id="my_date" type="text"
       class=...
       name="record[my_date]"
       value="{{isset($project)&&isset($project->my_date)?$project->my_date->format('Y-m-d'):null}}"
       placeholder="{{resolve('translate')->getWord('My Date')}}"
 > 

In migration

$table->dateTime('my_date');

I saw from this pots --> Date not being saved to mySQL datebase with laravel

And I tried --> I didn't get any error. Just when I delete & php artisan migrate & fill the font-end table again, there is still no date information being saved.

$table->dateTime('my_date')->format("Y-d-m");  

I saw from this post --> After upgrade to laravel 5.3 error invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'

And I tried --> no error, but still, no date information being saved.

$table->dateTime('pcmm_shipment_date')->default(NOW());

I also tried --> no error, but still, no date information being saved.

$table->datetime('expires_at')->nullable($value = true);
public function storeMyData(Request $request)
    {

        //validate
        $validator = Validator::make($request['record'],
            [
                'project_id' => 'required',
                'my_date' => 'required',
            ]
        );
        ...  
    }

Upvotes: 1

Views: 73

Answers (1)

party-ring
party-ring

Reputation: 1871

You can use useCurrent like so:

$table->timestamp('my_date')->useCurrent();

From the Laravel file vendor/laravel/framework/src/Illuminate/Database/Schema/ColumnDefinition.php it defines it as:

Set the TIMESTAMP column to use CURRENT_TIMESTAMP as default value

Upvotes: 0

Related Questions