###前提・実現したいこと
csvをDLしようとする際、必ず出力されるCSVの1行目を除く他すべての1列目が空になってしまいます。
具体的には、1列目に入るべき値が2列目に入ってしまっています。
また、一部こちらが望んだ並び順ではない並び順が出力されます。
CSVに出力されるものの並び順を確認するためにはどこをみればよいのでしょうか?
これらの問題を解決するためにご助力いただけないでしょうか。
何卒よろしくお願いいたします。
###該当のソースコード
php
1list ($titles, $value) = $this->titlesValue($type, $ids, $items); 2do { 3 $csv = storage_path().'/tmp/'.md5(uniqid(mt_rand(), true)).'.tmp'; 4 } while (file_exists($csvPath)); 5$buf = fopen($csv, 'w'); 6 fwrite($buf, "\xEF\xBB\xBF"); 7 fputcsv($buf, $titles); 8 foreach ($ids as $id) { 9 if (isset($valuess[$id])) { 10 fputcsv($buf, $valuess[$id]); 11 } 12 } 13fclose($buf); 14$file = (string)request()->input('file'); 15 return response()->download($csvPath, ($file!==''?$file:'export').'.csv')->deleteFileAfterSend(true);
php
1public function main($type) { 2 $komoji = substr($type, 0, 1); 3 $ids = $this->ids($type); 4 $items = array_map(function($c){return $c->toArray();}, call_user_func('\\App\\Models\\'.strtoupper($komoji).'item::uses')); 5return $this->download($type, $ids, $items); 6}
###追記
CSVの中身を確認しましたら、(1行目を除く)全ての1列目に入れるもののみ、全ての2列目に入っておりました。
その原因は、1列目と2列目のそれぞれのセルに該当する値を挿入するための指標であるvalueが同じ値であったためだと考えております。
php
1 public function titlesValuess($type, $ids, $items) { 2 $info = $this->info()[$type]; 3 $komoji = substr($type, 0, 1); 4 $titles = []; 5 $data = []; 6 if (request()->input($komoji.'item')) { 7 $input = array_unique(array_map('intval',(array)request()->input($komoji.'item'))); 8 foreach ($items as $i => $item) { 9 if (in_array($item['id'], $input, true)) { 10 $titles[] = $item['name']; 11 $item['index'] = count($titles) - 1; 12 $data['item'][$item['id']] = $item; 13 } 14 } 15 } 16 if (!$titles) return [$titles, []]; 17 $valuess = array_fill_keys($ids, array_fill(0, count($titles), null)); 18 return [$titles, $valuess]; 19 }
titlesValuessの$titlesにdd array:5 [▼ 0 => "A" 1 => "B" 2 => "C" 3 => "D" 4 => "E" 5 => "F" titlesValuessの$valuessにdd 0 => null 1 => "A'" 2 => "B'" 3 => "C'" 4 => "D'" 5 => "F'" 6 => "G'" ※一部合致している箇所と、合致していない箇所がある状況にあります。
###試したこと
listのあるところで「dd($value);」をかけてみたところ、上記の配列が結果として出てきました。
また、「dd($titles);」ではCSVに出力される最初の行にあたる項目名が出力されます。
###補足情報(言語/FW/ツール等のバージョンなど)
laravel 5.1