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>');
}
}
λͺ¨λΈμμ 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
]
}
λ΄ μμ
μμ λλ κ·Έλ° λ°μ΄νΈκ°
μ΄λ€ μκ°?
λ¬Έμ λ₯Ό λ°κ²¬νμ΅λλ€.
λ΄κ° λ£μ΄ 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,
]);
}
}
κ°μ₯ μ μ©ν λκΈ
λ μ§ μ΄μ λ μ§ νμμΌλ‘ μ§μ ν λ Carbonμ μ»μΌλ €λ©΄ :
Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date:: excelToDateTimeObject($row['created_at']))