Amazon linux 2 AMI上で、
laravel 6のアプリケーションを作成しています。
DBは、mysql(Ver 15.1 Distrib 10.2.38-MariaDB, for Linux (x86_64) using EditLine wrapper)
やりたいことは、CSVの内容をDBのテーブルにインサートしたいのです。
Maatwebsite/Excel 3.1を導入し、下記のようなコードを書いたのですが、エラーが出てしまいます。
(※ちなみに、windowsのxampp環境では、上手く行っております。)
【エラー内容】
SQLSTATE[HY000]: General error: 1364 Field 'month' doesn't have a default value (SQL: insert into Service_summariesImport
(updated_at
, created_at
) values (2022-04-27 02:47:09, 2022-04-27 02:47:09))_
【インポート用のCSV】
※ExcelのCSV形式で保存。文字コードはUTF-8。ヘッダは無し。
CSV
202201,900011,900001,111,10,1000000 202201,900011,900001,112,20,2000000
【ソースコード】
Service_summary.php(モデル)
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Service_summary extends Model { protected $fillable = [ 'month', 'hoken_number1', 'hoken_number2','service_code', 'user_number', 'sum', ]; }
Service_summariesImport.php(インポート)
<?php namespace App\Imports; use App\Service_summary; use Maatwebsite\Excel\Concerns\ToModel; class Service_summariesImport implements ToModel { /** * @param array $row * * @return \Illuminate\Database\Eloquent\Model|null */ public function model(array $row) { return new Service_summary([ 'month' => $row[0], 'hoken_number1' => $row[1], 'hoken_number2' => $row[2], 'service_code' => $row[3], 'user_number' => $row[4], 'sum' => $row[5] ]); } }
UploadController.php(コントローラ・抜粋)
foreach($_POST['chkbox'] as $value){ // zipファイルを解凍する $zip = new ZipArchive(); $zip_path = $upload_path . DIRECTORY_SEPARATOR . $value; //$zip_path = $upload_path . '\\' . $value; $result = $zip->open($zip_path); if($result === TRUE) { $zip->setPassword('test'); if($zip->extractTo($extract_path) === TRUE){ // csvファイルをDBへ登録する for($i = 0; $i < $zip->numFiles; $i++){ $file_name = $zip->getNameIndex($i); $file_path = $extract_path . DIRECTORY_SEPARATOR . $file_name; $import = new Service_summariesImport(); Excel::import($import, $file_path); // インポート用のコード } }
migrationファイル
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateServiceSummariesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('service_summaries', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); $table->integer('month'); $table->integer('hoken_number1'); $table->integer('hoken_number2'); $table->string('service_code'); $table->integer('user_number'); $table->bigInteger('sum'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('service_summaries'); } }
【試したこと】
調査し次のような手を打ちましたが、全てうまく行きませんでした。
①migrationファイルで、NGとなるカラム名をNULL許可にした。
$table->integer('カラム名')->nullable();
→冒頭エラーは解消されたがDBを見ると、CSVの項目がNULL値でインサートされてしまっている。
②migrationファイルで、NGとなるカラム名にデフォルト値を設定した。
$table->integer('カラム名')->default(NN);
→冒頭エラーは解消されたがDBを見ると、CSVの項目が入らず、全てデフォルト値がインサートされている。
③config/database.phpのstrictの値をfalseに設定した。
→冒頭エラーは解消されたがDBを見ると、全て0の値がインサートされている。
【現在の懸念点】
残る怪しい点としては、
config/excel.phpの中身をどこか変えれば上手く行くような気がしていますが、
検討が付きません。
config/excel.php
<?php use Maatwebsite\Excel\Excel; return [ ・・前半部分Exportsは略。 'imports' => [ /* |-------------------------------------------------------------------------- | Read Only |-------------------------------------------------------------------------- | | When dealing with imports, you might only be interested in the | data that the sheet exists. By default we ignore all styles, | however if you want to do some logic based on style data | you can enable it by setting read_only to false. | */ 'read_only' => true, /* |-------------------------------------------------------------------------- | Ignore Empty |-------------------------------------------------------------------------- | | When dealing with imports, you might be interested in ignoring | rows that have null values or empty strings. By default rows | containing empty strings or empty values are not ignored but can be | ignored by enabling the setting ignore_empty to true. | */ 'ignore_empty' => false, /* |-------------------------------------------------------------------------- | Heading Row Formatter |-------------------------------------------------------------------------- | | Configure the heading row formatter. | Available options: none|slug|custom | */ 'heading_row' => [ 'formatter' => 'slug', ], /* |-------------------------------------------------------------------------- | CSV Settings |-------------------------------------------------------------------------- | | Configure e.g. delimiter, enclosure and line ending for CSV imports. | */ 'csv' => [ 'delimiter' => null, 'enclosure' => '"', 'escape_character' => '\\', 'contiguous' => false, 'input_encoding' => 'UTF-8', ], /* |-------------------------------------------------------------------------- | Worksheet properties |-------------------------------------------------------------------------- | | Configure e.g. default title, creator, subject,... | */ 'properties' => [ 'creator' => '', 'lastModifiedBy' => '', 'title' => '', 'description' => '', 'subject' => '', 'keywords' => '', 'category' => '', 'manager' => '', 'company' => '', ], ], ・・中間略。 /* |-------------------------------------------------------------------------- | Transaction Handler |-------------------------------------------------------------------------- | | By default the import is wrapped in a transaction. This is useful | for when an import may fail and you want to retry it. With the | transactions, the previous import gets rolled-back. | | You can disable the transaction handler by setting this to null. | Or you can choose a custom made transaction handler here. | | Supported handlers: null|db | */ 'transactions' => [ 'handler' => 'db', 'db' => [ 'connection' => null, ], ], 'temporary_files' => [ /* |-------------------------------------------------------------------------- | Local Temporary Path |-------------------------------------------------------------------------- | | When exporting and importing files, we use a temporary file, before | storing reading or downloading. Here you can customize that path. | */ 'local_path' => storage_path('framework/cache/laravel-excel'), /* |-------------------------------------------------------------------------- | Remote Temporary Disk |-------------------------------------------------------------------------- | | When dealing with a multi server setup with queues in which you | cannot rely on having a shared local temporary path, you might | want to store the temporary file on a shared disk. During the | queue executing, we'll retrieve the temporary file from that | location instead. When left to null, it will always use | the local path. This setting only has effect when using | in conjunction with queued imports and exports. | */ 'remote_disk' => null, 'remote_prefix' => null, /* |-------------------------------------------------------------------------- | Force Resync |-------------------------------------------------------------------------- | | When dealing with a multi server setup as above, it's possible | for the clean up that occurs after entire queue has been run to only | cleanup the server that the last AfterImportJob runs on. The rest of the server | would still have the local temporary file stored on it. In this case your | local storage limits can be exceeded and future imports won't be processed. | To mitigate this you can set this config value to be true, so that after every | queued chunk is processed the local temporary file is deleted on the server that | processed it. | */ 'force_resync_remote' => null, ], ];
どなたかご教授頂けましたら幸いでございます。
まだ回答がついていません
会員登録して回答してみよう