2

I search all the related article but no solution. I was setting up a sitemap for Laravel when I realise the timestamp is not ok. So this is bad for SEO.

I always have those 0 in the created_at and updated_at field in the MySQL database.

I'm not sure if this is a Laravel, SQL or Excel question.

What I do:

  • migrate my table in laravel in my localhost $table->timestamps(); tried this also but no
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
$timestamps = false;
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
  • I import the sql database in the server phpmyadmin from my localhost

  • And import the excel csv file in phpmyadmin with the data.

Maybe there's another way to properly do this?

Joundill
  • 6,828
  • 12
  • 36
  • 50
Danny HC
  • 31
  • 5
  • In your MySQL table that column is properly working ? – Andrei Bunea Aug 23 '20 at 17:20
  • yes but its all zero when importing from an csv – Danny HC Aug 23 '20 at 17:42
  • Make sure that the date format is date is in this format: Y-m-d H:i:s or you can make a insert using a Controller and this $timestamp = date('Y-m-d H:i:s'); to test if the MySql column is set succesfully . If your format is not correct, it will display those 0's . – Andrei Bunea Aug 23 '20 at 23:47
  • 1
    yes i did resolve the problem. Laravel when migrating the table are not setting correctly. I had to manually import the data using maatwebsite for the date to appear correctly. But I will make notes of that for next time. I also add manually the primary key and autoincrement for all the id of my tables. So migrating from laravel 7 seem to have some problem. Anyway thanks – Danny HC Aug 25 '20 at 14:35
  • No problem, good luck! – Andrei Bunea Aug 25 '20 at 20:29

1 Answers1

0

If you are using maatwebsite/excel, and if you have created the timestamps through migration likewise, so the model will insert the timestamps automatically.

    $table->timestamps();

note: make sure that your table column created_at is not present in the fillable array of your model and you are not using the WithUpserts approach as mentioned in the documentation. see: https://docs.laravel-excel.com/3.1/imports/model.html

protected $fillable = [
         'users_id',
         'name',
         'created_at' //it should not be here
]

Another Approach with Carbon

you have to add the timestamps through the use of carbon.

First add the created_at in the fillable array of your model

 protected $fillable = [
          
             'name',
             'created_at' //must be present
    ]  

And then you have to use the carbon, go to your import folder and in your import file, bring the below changes which are marked with comments.

 <?php

namespace App\Imports;
use Carbon\Carbon; //add this line of code

use App\User;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;

class UsersImport implements ToModel
{
    /**
     * @param array $row
     *
     * @return User|null
     */
    public function model(array $row)
    {
        return new User([
           
           'created_at'=> now(),  //add this line of code
           'name'     => $row[0],
         

        ]);
    }
}

note: it is also applicable for the upserts, in upserts the maatwebsite is not able to add the timestamps it would be null, but with the above mentioned method you can store the timestamps, you can use the same method for updated_at.

Sabaoon Bedar
  • 3,113
  • 2
  • 31
  • 37