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
]
}
在我的Excel中,我有这样的约会
任何想法?
我发现了问题。
我将dd($row)
放在News::create
然后检查什么来自excel。
我又看到了41767。
然后,我转到excel并检查created_at字段的格式。 是日期。 但是,当我将格式从日期更改为文本时,我看到了41767。
这样做,我了解到,导入之前,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"
怎么了?
似乎我找到了解决方案。
唯一可行的方法是将日期2014-05-08
保留在Excel中并设置为文本格式。
因为它按此顺序存储在MySQL = YYYY-MM-DD中
这些都不起作用
2014年8月5日-错误的订单
2014年8月5日-错误的分隔符和错误的顺序
2014.05.08-分隔符错误
如果在我的案例日期2014-05-08中Excel中格式化为日期的日期字段在导入之前始终转换为41767。 我不知道为什么
我尝试了日期到时间戳转换器,它给了我
2014-05-08 = 1399507200。
2014年8月5日= 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']))
它适用于非空字段。 但是,当导入脚本到达空字段时,它将停止并显示以下错误。
此问题仅在Laravel 7中出现。使用Laravel 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']))