Laravel-excel: [BUG] λ‚ μ§œ ν•„λ“œλ₯Ό κ°€μ Έ μ˜€μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2020λ…„ 03μ›” 10일  Β·  14μ½”λ©˜νŠΈ  Β·  좜처: Maatwebsite/Laravel-Excel

μ „μ œ 쑰건

  • [Π₯] Laravel Excel 버전이 μ—¬μ „νžˆ μ§€μ›λ˜λŠ”μ§€ 확인 : https://docs.laravel-excel.com/3.1/getting-started/support.html#supported -versions
  • [Π₯] μ½”λ“œ μ™ΈλΆ€μ—μ„œ λ™μž‘μ„ μž¬ν˜„ ν•  μˆ˜μžˆλŠ” λ¬Έμ œλŠ” Laravel Excel둜 κ²©λ¦¬λ©λ‹ˆλ‹€.
  • [Π₯] λ¬Έμ œκ°€ 이미 μ œμΆœλ˜μ§€ μ•Šμ•˜λŠ”μ§€ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.
  • [Π₯]이 문제λ₯Ό ν•΄κ²°ν•˜λŠ” PR이 μ œμΆœλ˜μ§€ μ•Šμ•˜λŠ”μ§€ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.
  • [Π₯] 전체 문제 ν…œν”Œλ¦Ώ μž‘μ„±

버전

  • PHP 버전 : 7.2.19
  • 라 라벨 버전 : 6.17.1
  • νŒ¨ν‚€μ§€ 버전 : 3.1

기술

created_at ν•„λ“œλŠ” λ°μ΄ν„°λ² μ΄μŠ€λ‘œ κ°€μ Έ μ˜€μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λŒ€μ‹  phpmyadmin에 1970-01-01이 ν‘œμ‹œλ©λ‹ˆλ‹€.

μž¬ν˜„ 단계

namespace App\Imports;

use App\NewsPost;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class NewsImport implements ToCollection, WithHeadingRow

{
    public $rowCount = null;

    public function collection(Collection $rows)
    {
        News::truncate();

        foreach ($rows as $row) 
        { 
          if( !empty($row['category_id'])) {  

            NewsPost::create([

               'category_id'=> $row['category_id'],
               'title'      => $row['title'], 
               'sef'        => $row['sef'], 
               'introtext'  => $row['introtext'], 
               'fulltext'   => $row['fulltext'], 
               'image'      => $row['image'], 
               'metatitle'  => $row['metatitle'], 
               'metakey'    => $row['metakey'], 
               'metadesc'   => $row['metadesc'], 
               'hits'       => $row['hits'], 
               'created_at' => $row['created_at'], 

            ]);
          }

        }

      $this->rowCount = $rows->count();

    }

}
namespace App\Admin\Controllers;

use Illuminate\Support\Facades\DB;
use App\Imports\NewsPostImport;
use Maatwebsite\Excel\Facades\Excel;

class SeedController extends BaseAdminController
{

    public function index(Content $content)
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Excel::import($newsPost     = new NewsPostImport,       'import/news_posts.xlsm');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');

        return $content
            ->title('Import & Export')
            ->description('Laravel excel')
            ->withSuccess('Π˜ΠΌΠΏΠΎΡ€Ρ‚ Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ', 
               '<p>Π˜ΠΌΠΏΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΎ '. $newsPost->rowCount.' новостСй</p>');
    }

}

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚ μ§œ 열을 λ‚ μ§œ ν˜•μ‹μœΌλ‘œ 지정할 λ•Œ Carbon을 μ–»μœΌλ €λ©΄ : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date:: excelToDateTimeObject($row['created_at']))

λͺ¨λ“  14 λŒ“κΈ€

λͺ¨λΈμ—μ„œ created_atλ₯Ό μ±„μšΈ 수 μžˆμŠ΅λ‹ˆκΉŒ?

예. λͺ¨λ“  ν•„λ“œλ₯Ό μ±„μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€.

dd ()λ₯Ό μ‹œλ„ν•˜κ³  거기에 무엇이 μžˆλŠ”μ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€.

지연에 λŒ€ν•œ 유감. λ‚˜λŠ” νœ΄κ°€ μ€‘μ΄μ—ˆλ‹€.
여기에 ddλ₯Ό λ„£μ–΄

            if (empty($row['slug'])) {
              News::create([
                 'category_id'  => $row['category_id'],
                 'title'        => $row['title'],
                 'slug'         => Str::slug($row['title'], '-'),
                 'fulltext'     => $row['fulltext'],
                 'image'        => $row['image'],
                 'metatitle'    => $row['metatitle'],
                 'metakey'      => $row['metakey'],
                 'metadesc'     => $row['metadesc'],
                 'hits'         => $row['hits'],
                 'published_at' => $row['published_at'],
                 'created_at'   => $row['created_at'],
                 'updated_at'   => $row['updated_at'],
                 'deleted_at'   => $row['deleted_at'],
              ]);
              dd($row);
            }

νŽ˜μ΄μ§€μ— μ΄μƒν•œ λ‚ μ§œ 좜λ ₯이 ν‘œμ‹œλ©λ‹ˆλ‹€.

Illuminate\Support\Collection {#1986 β–Ό
  #items: array:14 [β–Ό
    "category_id" => 4
    .......
    "published_at" => null
    "created_at" => 41767
  ]
}

λ‚΄ μ—‘μ…€μ—μ„œ λ‚˜λŠ” 그런 λ°μ΄νŠΈκ°€
date

μ–΄λ–€ 생각?

문제λ₯Ό λ°œκ²¬ν–ˆμŠ΅λ‹ˆλ‹€.
λ‚΄κ°€ λ„£μ–΄ dd($row) ν•˜κΈ° 전에 News::create 와 μ—‘μ…€μ—μ„œ μ˜€λŠ” 것을 확인.
그리고 41767을 λ‹€μ‹œ λ΄€μŠ΅λ‹ˆλ‹€.
그런 λ‹€μŒ Excel둜 μ΄λ™ν•˜μ—¬ created_at ν•„λ“œμ˜ ν˜•μ‹μ„
κ°€μ Έ 였기 전에 Excel의 λͺ¨λ“  λ‚ μ§œκ°€ ν…μŠ€νŠΈλ‘œ λ³€ν™˜λœλ‹€λŠ” 것을 μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œμ΄ ν•„λ“œλ₯Ό μˆ˜λ™μœΌλ‘œ ν…μŠ€νŠΈλ‘œ λ³€ν™˜ν•˜κ³  λ‚ μ§œλ₯Ό μž…λ ₯ν•˜λ©΄ 이제 μž‘λ™ν•©λ‹ˆλ‹€.

ν•œ 번만 μ„±κ³΅μ μœΌλ‘œ κ°€μ Έ 였기λ₯Ό ν•  수 μžˆμ—ˆκ³  μ—¬μ „νžˆ λͺ‡ 가지 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. 이해할 수 μ—†μŠ΅λ‹ˆλ‹€.
λ‚ μ§œλ₯Ό κ°€μ Έ μ˜€λŠ” 것은 골치 μ•„ν”ˆ μΌμž…λ‹ˆλ‹€. 항상 μž‘λ™ν•˜λŠ” κ·œμΉ™μ„ λ³Ό 수 μ—†μŠ΅λ‹ˆλ‹€.

κ°€μ Έ 였기 전에 Excelμ—μ„œ λ‚ μ§œλ₯Ό μœ μ§€ν•˜λŠ” 방법을 λͺ‡ 번 ν…ŒμŠ€νŠΈν–ˆλŠ”λ° μ•ˆμ •μ μΈ λ™μž‘μ΄ μ•„λ‹Œ κ²ƒμœΌλ‘œ λ‚˜νƒ€λ‚¬μŠ΅λ‹ˆλ‹€.

λͺ¨λ“  λ‚ μ§œ ν•„λ“œ ν˜•μ‹ = ν…μŠ€νŠΈλ₯Ό Excel둜 λ§Œλ“­λ‹ˆλ‹€.
08-05-2014 λ‚ μ§œλ₯Ό μž…λ ₯ν•˜κ³  ν…μŠ€νŠΈ ν˜•μ‹μ„ 지정할 λ•Œ.
μ–΄μ¨Œλ“  κ°€μ Έ μ˜€λŠ” λ™μ•ˆ 였λ₯˜κ°€ μžˆμŠ΅λ‹ˆλ‹€.

Unexpected data found.
 Unexpected data found.
 Data missing
http://localhost.test/admin/seed

\ vendor \ nesbot \ carbon \ src \ Carbon \ Traits \ Creatorλ₯Ό 가리 ν‚΅λ‹ˆλ‹€. php : 623

        if (static::isStrictModeEnabled()) {
            throw new InvalidArgumentException(implode(PHP_EOL, $lastErrors['errors']));
        }

ν•˜μ§€λ§Œ dd($row)

 "created_at" => "08-05-2014"

뭐가 잘λͺ» λ˜μ—ˆ λ‹ˆ?

λ‚΄κ°€ 해결책을 찾은 것 κ°™μŠ΅λ‹ˆλ‹€.
μœ μΌν•œ μž‘μ—… 방법은 Excelμ—μ„œ 2014-05-08 λ‚ μ§œλ₯Ό μœ μ§€ν•˜κ³  ν…μŠ€νŠΈ ν˜•μ‹μœΌλ‘œ μ§€μ •ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.
이 μˆœμ„œλŒ€λ‘œ MySQL = YYYY-MM-DD에 μ €μž₯되기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
이듀 쀑 μ–΄λŠ 것도 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
08-05-2014-잘λͺ»λœ μ£Όλ¬Έ
2014 λ…„ 5 μ›” 8 일-잘λͺ»λœ ꡬ뢄 기호 및 잘λͺ»λœ μˆœμ„œ
2014.05.08-잘λͺ»λœ κ΅¬λΆ„μž

λ‚΄ 경우 λ‚ μ§œ 2014-05-08μ—μ„œ Excelμ—μ„œ λ‚ μ§œλ‘œ ν˜•μ‹μ΄ μ§€μ •λœ λ‚ μ§œ ν•„λ“œλŠ” κ°€μ Έ 였기 전에 항상 41767둜 λ³€ν™˜λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 이유λ₯Ό λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.
νƒ€μž„ μŠ€νƒ¬ν”„ λ³€ν™˜κΈ°μ— λ‚ μ§œλ₯Ό μ‹œλ„ν–ˆλŠ”λ°
2014-05-08 = 1399507200.
2014 λ…„ 5 μ›” 8 일 = 1407182400

41767을 얻을 μˆ˜μžˆλŠ” μœ μΌν•œ 방법은 Excel μˆ˜μ‹ =text(08.05.2014;0) μ—μ„œ μ‹œλ„ ν•  λ•Œμž…λ‹ˆλ‹€.
κ·Έλž˜μ„œ Excel둜 λ³€ν™˜λ˜μ—ˆλ‹€κ³  κ°€μ •ν•©λ‹ˆλ‹€.
그리고 λ‚ μ§œ ν•„λ“œλ₯Ό YYYY-MM-DD ν˜•μ‹μœΌλ‘œ 쑰정해도 λ³€ν™˜λ©λ‹ˆλ‹€.

λ”°λΌμ„œ μž‘λ™ν•˜λŠ” μ†”λ£¨μ…˜μ€ YYYY-MM-DD ν…μŠ€νŠΈκ°€ 포함 된 ν…μŠ€νŠΈ ν˜•μ‹μž…λ‹ˆλ‹€.
이 μ†”λ£¨μ…˜μ΄ μ™„μ „νžˆ μ˜³μ§€ μ•Šλ‹€λ©΄ μ •μ •ν•˜μ‹­μ‹œμ˜€.

λ‚˜λŠ” 같은 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. 'λ‚ μ§œ'ν•„λ“œλŠ” 값이 μ•„λ‹Œ 숫자 (제 생각에 μ°Έμ‘°)λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

λ‚ μ§œ 열을 λ‚ μ§œ ν˜•μ‹μœΌλ‘œ 지정할 λ•Œ Carbon을 μ–»μœΌλ €λ©΄ : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date:: excelToDateTimeObject($row['created_at']))

λΉ„μ–΄ μžˆμ§€ μ•Šμ€ ν•„λ“œμ—μ„œ μž‘λ™ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ κ°€μ Έ 였기 μŠ€ν¬λ¦½νŠΈκ°€ 빈 ν•„λ“œμ— λ„λ‹¬ν•˜λ©΄ λ‹€μŒ 였λ₯˜μ™€ ν•¨κ»˜ μ€‘μ§€λ©λ‹ˆλ‹€.
이 λ¬Έμ œλŠ” 라 라벨 7μ—μ„œλ§Œ λ‚˜νƒ€λ‚©λ‹ˆλ‹€. 라 라벨 6μ—μ„œλŠ”μ΄ λ¬Έμ œκ°€ μ—†μŠ΅λ‹ˆλ‹€.

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1970-01-01 00:00:00' for column 'published_at' at row 1 (SQL: insert into `news_posts` (`category_id`, `title`, `slug`, `fulltext`, `image`, `metatitle`, `metakey`, `metadesc`, `hits`, `is_published`, `published_at`, `created_at`, `updated_at`, `deleted_at`) values ...

λ¬΄μ—‡μ„ν•΄μ•Όν•©λ‹ˆκΉŒ?
κ°€μ Έ 였기 λ‚΄μ—μ„œ IF μ ˆμ„ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

                if (empty($row['published_at'])) { 
                    'published_at' => $row['published_at'],
                } else {
                 'published_at' => Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['published_at'])),
                }

syntax error, unexpected 'if' (T_IF), expecting ']' 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

μž„μ‹œ λ³€μˆ˜μ— λ„£μ–΄

"μž„μ‹œ λ³€μˆ˜"μ—μ„œ 무슨 λœ»μž…λ‹ˆκΉŒ?

ν—ˆμš©λ˜λŠ” 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.

        foreach ($rows as $row) 
        {  
          if (!empty($row['category_id'])) {
            $slug = empty($row['slug']) ? Str::slug($row['title'], '-') : $row['slug'];
            $published_at = empty($row['published_at']) ? $row['published_at'] : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['published_at']));
            $created_at = empty($row['created_at']) ? $row['created_at'] : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['created_at']));
            $updated_at = empty($row['updated_at']) ? $row['updated_at'] : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['updated_at']));
            $deleted_at = empty($row['deleted_at']) ? $row['deleted_at'] : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['deleted_at']));

              NewsPost::create([
                 'category_id'  => $row['category_id'],
                 'title'        => $row['title'],
                 'slug'         => $slug,
                 'fulltext'     => $row['fulltext'],
                 'image'        => $row['image'],
                 'metatitle'    => $row['metatitle'],
                 'metakey'      => $row['metakey'],
                 'metadesc'     => $row['metadesc'],
                 'hits'         => $row['hits'],
                 'is_published' => $row['is_published'],
                 'published_at' => $published_at,
                 'created_at'   => $created_at,
                 'updated_at'   => $updated_at,
                 'deleted_at'   => $deleted_at,
              ]);             

          }

        }
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰