質問編集履歴
7
情報追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
_【エラー内容】_
|
10
10
|
---
|
11
11
|
|
12
|
-
SQLSTATE[HY000]: General error: 1364 Field '
|
12
|
+
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))_
|
13
13
|
|
14
14
|
_【インポート用のCSV】_
|
15
15
|
---
|
6
加筆
test
CHANGED
File without changes
|
test
CHANGED
@@ -159,6 +159,7 @@
|
|
159
159
|
---
|
160
160
|
残る怪しい点としては、
|
161
161
|
config/excel.phpの中身をどこか変えれば上手く行くような気がしていますが、
|
162
|
+
検討が付きません。
|
162
163
|
|
163
164
|
```config/excel.php
|
164
165
|
<?php
|
5
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,20 +6,21 @@
|
|
6
6
|
Maatwebsite/Excel 3.1を導入し、下記のようなコードを書いたのですが、エラーが出てしまいます。
|
7
7
|
(※ちなみに、windowsのxampp環境では、上手く行っております。)
|
8
8
|
|
9
|
+
_【エラー内容】_
|
9
|
-
---
|
10
|
+
---
|
10
|
-
|
11
|
+
|
11
12
|
SQLSTATE[HY000]: General error: 1364 Field 'カラム名' doesn't have a default value (SQL: insert into `テーブル名` (`updated_at`, `created_at`) values (2022-04-27 02:47:09, 2022-04-27 02:47:09))_
|
12
13
|
|
14
|
+
_【インポート用のCSV】_
|
13
|
-
---
|
15
|
+
---
|
14
|
-
|
16
|
+
※ExcelのCSV形式で保存。文字コードはUTF-8。ヘッダは無し。
|
15
17
|
```CSV
|
16
18
|
202201,900011,900001,111,10,1000000
|
17
19
|
202201,900011,900001,112,20,2000000
|
18
|
-
202201,900011,900001,113,30,3000000
|
19
|
-
202201,900011,900001,114,40,4000000
|
20
|
-
```
|
20
|
+
```
|
21
|
-
|
22
21
|
_【ソースコード】_
|
22
|
+
---
|
23
|
+
|
23
24
|
```Service_summary.php(モデル)
|
24
25
|
<?php
|
25
26
|
|
@@ -132,6 +133,7 @@
|
|
132
133
|
|
133
134
|
```
|
134
135
|
|
136
|
+
【試したこと】
|
135
137
|
---
|
136
138
|
調査し次のような手を打ちましたが、全てうまく行きませんでした。
|
137
139
|
|
@@ -153,18 +155,180 @@
|
|
153
155
|
|
154
156
|
→冒頭エラーは解消されたがDBを見ると、全て0の値がインサートされている。
|
155
157
|
|
156
|
-
|
158
|
+
【現在の懸念点】
|
157
|
-
---
|
159
|
+
---
|
160
|
+
残る怪しい点としては、
|
158
|
-
|
161
|
+
config/excel.phpの中身をどこか変えれば上手く行くような気がしていますが、
|
162
|
+
|
163
|
+
```config/excel.php
|
159
|
-
|
164
|
+
<?php
|
165
|
+
|
166
|
+
use Maatwebsite\Excel\Excel;
|
167
|
+
|
168
|
+
return [
|
169
|
+
|
170
|
+
・・前半部分Exportsは略。
|
171
|
+
|
172
|
+
'imports' => [
|
173
|
+
|
174
|
+
/*
|
175
|
+
|--------------------------------------------------------------------------
|
176
|
+
| Read Only
|
177
|
+
|--------------------------------------------------------------------------
|
178
|
+
|
|
179
|
+
| When dealing with imports, you might only be interested in the
|
180
|
+
| data that the sheet exists. By default we ignore all styles,
|
181
|
+
| however if you want to do some logic based on style data
|
182
|
+
| you can enable it by setting read_only to false.
|
183
|
+
|
|
184
|
+
*/
|
185
|
+
'read_only' => true,
|
186
|
+
|
187
|
+
/*
|
188
|
+
|--------------------------------------------------------------------------
|
189
|
+
| Ignore Empty
|
190
|
+
|--------------------------------------------------------------------------
|
191
|
+
|
|
192
|
+
| When dealing with imports, you might be interested in ignoring
|
193
|
+
| rows that have null values or empty strings. By default rows
|
194
|
+
| containing empty strings or empty values are not ignored but can be
|
195
|
+
| ignored by enabling the setting ignore_empty to true.
|
196
|
+
|
|
197
|
+
*/
|
198
|
+
'ignore_empty' => false,
|
199
|
+
|
200
|
+
/*
|
201
|
+
|--------------------------------------------------------------------------
|
202
|
+
| Heading Row Formatter
|
203
|
+
|--------------------------------------------------------------------------
|
204
|
+
|
|
205
|
+
| Configure the heading row formatter.
|
206
|
+
| Available options: none|slug|custom
|
207
|
+
|
|
208
|
+
*/
|
209
|
+
'heading_row' => [
|
210
|
+
'formatter' => 'slug',
|
211
|
+
],
|
212
|
+
|
213
|
+
/*
|
214
|
+
|--------------------------------------------------------------------------
|
215
|
+
| CSV Settings
|
216
|
+
|--------------------------------------------------------------------------
|
217
|
+
|
|
218
|
+
| Configure e.g. delimiter, enclosure and line ending for CSV imports.
|
219
|
+
|
|
220
|
+
*/
|
221
|
+
'csv' => [
|
222
|
+
'delimiter' => null,
|
223
|
+
'enclosure' => '"',
|
224
|
+
'escape_character' => '\\',
|
225
|
+
'contiguous' => false,
|
226
|
+
'input_encoding' => 'UTF-8',
|
227
|
+
],
|
228
|
+
|
229
|
+
/*
|
230
|
+
|--------------------------------------------------------------------------
|
231
|
+
| Worksheet properties
|
232
|
+
|--------------------------------------------------------------------------
|
233
|
+
|
|
234
|
+
| Configure e.g. default title, creator, subject,...
|
235
|
+
|
|
236
|
+
*/
|
237
|
+
'properties' => [
|
238
|
+
'creator' => '',
|
239
|
+
'lastModifiedBy' => '',
|
240
|
+
'title' => '',
|
241
|
+
'description' => '',
|
242
|
+
'subject' => '',
|
243
|
+
'keywords' => '',
|
244
|
+
'category' => '',
|
245
|
+
'manager' => '',
|
246
|
+
'company' => '',
|
247
|
+
],
|
248
|
+
|
249
|
+
],
|
250
|
+
|
251
|
+
・・中間略。
|
252
|
+
|
253
|
+
/*
|
254
|
+
|--------------------------------------------------------------------------
|
255
|
+
| Transaction Handler
|
256
|
+
|--------------------------------------------------------------------------
|
257
|
+
|
|
258
|
+
| By default the import is wrapped in a transaction. This is useful
|
259
|
+
| for when an import may fail and you want to retry it. With the
|
260
|
+
| transactions, the previous import gets rolled-back.
|
261
|
+
|
|
262
|
+
| You can disable the transaction handler by setting this to null.
|
263
|
+
| Or you can choose a custom made transaction handler here.
|
264
|
+
|
|
265
|
+
| Supported handlers: null|db
|
266
|
+
|
|
267
|
+
*/
|
268
|
+
'transactions' => [
|
269
|
+
'handler' => 'db',
|
270
|
+
'db' => [
|
271
|
+
'connection' => null,
|
272
|
+
],
|
273
|
+
],
|
274
|
+
|
275
|
+
'temporary_files' => [
|
276
|
+
|
277
|
+
/*
|
278
|
+
|--------------------------------------------------------------------------
|
279
|
+
| Local Temporary Path
|
280
|
+
|--------------------------------------------------------------------------
|
281
|
+
|
|
282
|
+
| When exporting and importing files, we use a temporary file, before
|
283
|
+
| storing reading or downloading. Here you can customize that path.
|
284
|
+
|
|
285
|
+
*/
|
286
|
+
'local_path' => storage_path('framework/cache/laravel-excel'),
|
287
|
+
|
288
|
+
/*
|
289
|
+
|--------------------------------------------------------------------------
|
290
|
+
| Remote Temporary Disk
|
291
|
+
|--------------------------------------------------------------------------
|
292
|
+
|
|
293
|
+
| When dealing with a multi server setup with queues in which you
|
294
|
+
| cannot rely on having a shared local temporary path, you might
|
295
|
+
| want to store the temporary file on a shared disk. During the
|
296
|
+
| queue executing, we'll retrieve the temporary file from that
|
297
|
+
| location instead. When left to null, it will always use
|
298
|
+
| the local path. This setting only has effect when using
|
299
|
+
| in conjunction with queued imports and exports.
|
300
|
+
|
|
301
|
+
*/
|
302
|
+
'remote_disk' => null,
|
303
|
+
'remote_prefix' => null,
|
304
|
+
|
305
|
+
/*
|
306
|
+
|--------------------------------------------------------------------------
|
307
|
+
| Force Resync
|
308
|
+
|--------------------------------------------------------------------------
|
309
|
+
|
|
310
|
+
| When dealing with a multi server setup as above, it's possible
|
311
|
+
| for the clean up that occurs after entire queue has been run to only
|
312
|
+
| cleanup the server that the last AfterImportJob runs on. The rest of the server
|
313
|
+
| would still have the local temporary file stored on it. In this case your
|
314
|
+
| local storage limits can be exceeded and future imports won't be processed.
|
315
|
+
| To mitigate this you can set this config value to be true, so that after every
|
316
|
+
| queued chunk is processed the local temporary file is deleted on the server that
|
317
|
+
| processed it.
|
318
|
+
|
|
319
|
+
*/
|
320
|
+
'force_resync_remote' => null,
|
321
|
+
],
|
322
|
+
];
|
323
|
+
|
324
|
+
```
|
325
|
+
|
160
326
|
どなたかご教授頂けましたら幸いでございます。
|
161
|
-
|
327
|
+
|
162
|
-
|
163
|
-
|
328
|
+
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
329
|
+
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
|
4
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
Maatwebsite/Excel 3.1を導入し、下記のようなコードを書いたのですが、エラーが出てしまいます。
|
7
7
|
(※ちなみに、windowsのxampp環境では、上手く行っております。)
|
8
8
|
|
9
|
-
|
9
|
+
---
|
10
10
|
__【エラー内容】_
|
11
11
|
SQLSTATE[HY000]: General error: 1364 Field 'カラム名' doesn't have a default value (SQL: insert into `テーブル名` (`updated_at`, `created_at`) values (2022-04-27 02:47:09, 2022-04-27 02:47:09))_
|
12
12
|
|
3
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,8 +10,16 @@
|
|
10
10
|
__【エラー内容】_
|
11
11
|
SQLSTATE[HY000]: General error: 1364 Field 'カラム名' doesn't have a default value (SQL: insert into `テーブル名` (`updated_at`, `created_at`) values (2022-04-27 02:47:09, 2022-04-27 02:47:09))_
|
12
12
|
|
13
|
+
---
|
14
|
+
_【インポート用のCSV】※ExcelのCSV形式で保存。文字コードはUTF-8。ヘッダは無し。_
|
15
|
+
```CSV
|
16
|
+
202201,900011,900001,111,10,1000000
|
17
|
+
202201,900011,900001,112,20,2000000
|
18
|
+
202201,900011,900001,113,30,3000000
|
19
|
+
202201,900011,900001,114,40,4000000
|
20
|
+
```
|
13
21
|
|
14
|
-
|
22
|
+
_【ソースコード】_
|
15
23
|
```Service_summary.php(モデル)
|
16
24
|
<?php
|
17
25
|
|
2
書式の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,45 +3,13 @@
|
|
3
3
|
DBは、mysql(Ver 15.1 Distrib 10.2.38-MariaDB, for Linux (x86_64) using EditLine wrapper)
|
4
4
|
|
5
5
|
やりたいことは、CSVの内容をDBのテーブルにインサートしたいのです。
|
6
|
-
Maatwebsite/Excel 3.1を導入し、
|
6
|
+
Maatwebsite/Excel 3.1を導入し、下記のようなコードを書いたのですが、エラーが出てしまいます。
|
7
|
-
|
8
|
-
Excel::import(new ●●Import() $file_path); // インポート用のコード
|
9
|
-
|
10
7
|
(※ちなみに、windowsのxampp環境では、上手く行っております。)
|
11
8
|
|
12
|
-
------------------------------------------------------------------
|
13
|
-
【エラー内容】
|
14
9
|
|
10
|
+
__【エラー内容】_
|
15
|
-
SQLSTATE[HY000]: General error: 1364 Field 'カラム名' doesn't have a default value (SQL: insert into `テーブル名` (`updated_at`, `created_at`) values (2022-04-27 02:47:09, 2022-04-27 02:47:09))
|
11
|
+
SQLSTATE[HY000]: General error: 1364 Field 'カラム名' doesn't have a default value (SQL: insert into `テーブル名` (`updated_at`, `created_at`) values (2022-04-27 02:47:09, 2022-04-27 02:47:09))_
|
16
|
-
------------------------------------------------------------------
|
17
12
|
|
18
|
-
調査し次のような手を打ちましたが、全てうまく行きませんでした。
|
19
|
-
|
20
|
-
---------------
|
21
|
-
①migrationファイルで、NGとなるカラム名をNULL許可にした。
|
22
|
-
|
23
|
-
$table->integer('カラム名')->nullable();
|
24
|
-
|
25
|
-
→冒頭エラーは解消されたがDBを見ると、CSVの項目がNULL値でインサートされてしまっている。
|
26
|
-
|
27
|
-
②migrationファイルで、NGとなるカラム名にデフォルト値を設定した。
|
28
|
-
|
29
|
-
$table->integer('カラム名')->default(NN);
|
30
|
-
|
31
|
-
→冒頭エラーは解消されたがDBを見ると、CSVの項目が入らず、全てデフォルト値がインサートされている。
|
32
|
-
|
33
|
-
③config/database.phpのstrictの値をfalseに設定する。
|
34
|
-
|
35
|
-
→冒頭エラーは解消されたがDBを見ると、全て0の値がインサートされている。
|
36
|
-
|
37
|
-
-----------------
|
38
|
-
|
39
|
-
残るは、config/excel.phpの中身をどこか変えれば上手く行くような気がしていますが、
|
40
|
-
検討がつきません。
|
41
|
-
どなたかご教授頂けましたら幸いでございます。
|
42
|
-
どうぞ宜しくお願い致します。
|
43
|
-
|
44
|
-
ご覧くださり、ありがとうございます。
|
45
13
|
|
46
14
|
|
47
15
|
```Service_summary.php(モデル)
|
@@ -109,7 +77,7 @@
|
|
109
77
|
$file_path = $extract_path . DIRECTORY_SEPARATOR . $file_name;
|
110
78
|
$import = new Service_summariesImport();
|
111
79
|
|
112
|
-
Excel::import($import, $file_path);
|
80
|
+
Excel::import($import, $file_path); // インポート用のコード
|
113
81
|
}
|
114
82
|
}
|
115
83
|
```
|
@@ -156,6 +124,39 @@
|
|
156
124
|
|
157
125
|
```
|
158
126
|
|
127
|
+
---
|
128
|
+
調査し次のような手を打ちましたが、全てうまく行きませんでした。
|
129
|
+
|
130
|
+
|
131
|
+
### ①migrationファイルで、NGとなるカラム名をNULL許可にした。
|
132
|
+
|
133
|
+
$table->integer('カラム名')->nullable();
|
134
|
+
|
135
|
+
→冒頭エラーは解消されたがDBを見ると、CSVの項目がNULL値でインサートされてしまっている。
|
136
|
+
|
137
|
+
### ②migrationファイルで、NGとなるカラム名にデフォルト値を設定した。
|
138
|
+
|
139
|
+
|
140
|
+
$table->integer('カラム名')->default(NN);
|
141
|
+
|
142
|
+
→冒頭エラーは解消されたがDBを見ると、CSVの項目が入らず、全てデフォルト値がインサートされている。
|
143
|
+
|
144
|
+
### ③config/database.phpのstrictの値をfalseに設定した。
|
145
|
+
|
146
|
+
→冒頭エラーは解消されたがDBを見ると、全て0の値がインサートされている。
|
147
|
+
|
148
|
+
|
149
|
+
---
|
150
|
+
残るは、config/excel.phpの中身をどこか変えれば上手く行くような気がしていますが、
|
151
|
+
検討がつきません。
|
152
|
+
どなたかご教授頂けましたら幸いでございます。
|
153
|
+
どうぞ宜しくお願い致します。
|
154
|
+
|
155
|
+
ご覧くださり、ありがとうございます。
|
159
156
|
|
160
157
|
|
161
158
|
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
1
ソースの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -41,6 +41,121 @@
|
|
41
41
|
どなたかご教授頂けましたら幸いでございます。
|
42
42
|
どうぞ宜しくお願い致します。
|
43
43
|
|
44
|
+
ご覧くださり、ありがとうございます。
|
45
|
+
|
46
|
+
|
47
|
+
```Service_summary.php(モデル)
|
48
|
+
<?php
|
49
|
+
|
50
|
+
namespace App;
|
51
|
+
|
52
|
+
use Illuminate\Database\Eloquent\Model;
|
53
|
+
|
54
|
+
class Service_summary extends Model
|
55
|
+
{
|
56
|
+
|
57
|
+
protected $fillable = [
|
58
|
+
'month', 'hoken_number1', 'hoken_number2','service_code', 'user_number', 'sum',
|
59
|
+
];
|
60
|
+
}
|
61
|
+
```
|
62
|
+
|
63
|
+
```Service_summariesImport.php(インポート)
|
64
|
+
<?php
|
65
|
+
|
66
|
+
namespace App\Imports;
|
67
|
+
|
68
|
+
use App\Service_summary;
|
69
|
+
use Maatwebsite\Excel\Concerns\ToModel;
|
70
|
+
|
71
|
+
class Service_summariesImport implements ToModel
|
72
|
+
{
|
73
|
+
/**
|
74
|
+
* @param array $row
|
75
|
+
*
|
76
|
+
* @return \Illuminate\Database\Eloquent\Model|null
|
77
|
+
*/
|
78
|
+
public function model(array $row)
|
79
|
+
{
|
80
|
+
return new Service_summary([
|
81
|
+
|
82
|
+
'month' => $row[0],
|
83
|
+
'hoken_number1' => $row[1],
|
84
|
+
'hoken_number2' => $row[2],
|
85
|
+
'service_code' => $row[3],
|
86
|
+
'user_number' => $row[4],
|
87
|
+
'sum' => $row[5]
|
88
|
+
]);
|
89
|
+
}
|
90
|
+
}
|
91
|
+
```
|
92
|
+
|
93
|
+
```UploadController.php(コントローラ・抜粋)
|
94
|
+
foreach($_POST['chkbox'] as $value){
|
95
|
+
// zipファイルを解凍する
|
96
|
+
$zip = new ZipArchive();
|
97
|
+
$zip_path = $upload_path . DIRECTORY_SEPARATOR . $value;
|
98
|
+
//$zip_path = $upload_path . '\\' . $value;
|
99
|
+
|
100
|
+
$result = $zip->open($zip_path);
|
101
|
+
if($result === TRUE) {
|
102
|
+
|
103
|
+
$zip->setPassword('test');
|
104
|
+
if($zip->extractTo($extract_path) === TRUE){
|
105
|
+
|
106
|
+
// csvファイルをDBへ登録する
|
107
|
+
for($i = 0; $i < $zip->numFiles; $i++){
|
108
|
+
$file_name = $zip->getNameIndex($i);
|
109
|
+
$file_path = $extract_path . DIRECTORY_SEPARATOR . $file_name;
|
110
|
+
$import = new Service_summariesImport();
|
111
|
+
|
112
|
+
Excel::import($import, $file_path);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
```migrationファイル
|
118
|
+
<?php
|
119
|
+
|
120
|
+
use Illuminate\Database\Migrations\Migration;
|
121
|
+
use Illuminate\Database\Schema\Blueprint;
|
122
|
+
use Illuminate\Support\Facades\Schema;
|
123
|
+
|
124
|
+
class CreateServiceSummariesTable extends Migration
|
125
|
+
{
|
126
|
+
/**
|
127
|
+
* Run the migrations.
|
128
|
+
*
|
129
|
+
* @return void
|
130
|
+
*/
|
131
|
+
public function up()
|
132
|
+
{
|
133
|
+
Schema::create('service_summaries', function (Blueprint $table) {
|
134
|
+
$table->bigIncrements('id');
|
135
|
+
$table->timestamps();
|
136
|
+
|
137
|
+
$table->integer('month');
|
138
|
+
$table->integer('hoken_number1');
|
139
|
+
$table->integer('hoken_number2');
|
140
|
+
$table->string('service_code');
|
141
|
+
$table->integer('user_number');
|
142
|
+
$table->bigInteger('sum');
|
143
|
+
});
|
144
|
+
}
|
145
|
+
|
146
|
+
/**
|
147
|
+
* Reverse the migrations.
|
148
|
+
*
|
149
|
+
* @return void
|
150
|
+
*/
|
151
|
+
public function down()
|
152
|
+
{
|
153
|
+
Schema::dropIfExists('service_summaries');
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
```
|
44
158
|
|
45
159
|
|
46
160
|
|
161
|
+
|