Laravel5.6を使って名刺管理アプリを作成しています。
名刺情報はMYSQLのデータベースに入れ、検索する機能を付けました。
そこで検索した名刺のリストをCSVファイルにダウンロードしたいのですが、うまくいきません。
ダミーのデータを使い、CSVファイル自体のダウンロードはできます。
変数$meishilistにデータベースの検索値を入れ、それをCSVファイルでダウンロードしようとすると、空のCSVファイルがダウンロードされます。
変数$meishilistの値を参照できないのが問題なのではないかと思います。
変数のスコープの問題かと思い、検索の各メソッドで、$meishilistのグローバル宣言をしましたが、
それでもうまくダウンロードできません。
コントローラ側のソースコードは以下の通りです。
php
1//最初のリスト画面を表示する 2 public function list() 3 { 4 global $meishilist; 5 $query = Meishi::query(); 6 $meishilist = $query->orderBy('id')->simplePaginate(50); 7 8 return view('list')->with('meishilist', $meishilist); 9 } 10 11 //list画面にて各ボタンが押された時の機能分け 12 public function postlist(Request $request) 13 { 14 15 if (Input::get('namesearch')) 16 { 17 return $this->namesearch($request); 18 } elseif (Input::get('companysearch')) { 19 return $this->companysearch($request); 20 } elseif (Input::get('othersearch')) { 21 return $this->othersearch($request); 22 } elseif (Input::get('csvdownload')) { 23 return $this->csvdownload($meishilist); 24 } elseif (Input::get('newregister')) { 25 return $this->form(); 26 } 27 } 28 29 //list画面での名前検索欄の機能・名刺情報「お名前」での検索を行う。 30 public function namesearch(Request $request) 31 { 32 global $meishilist; 33 $param = "$request->namesearch"; 34 $nameresult = Meishi::where('name', 'like', "%{$param}%")->simplePaginate(50); 35 $meishilist = $nameresult; 36 return view('list')->with('meishilist', $meishilist); 37 } 38 39 //list画面での会社名検索欄の機能・名刺情報「会社名」での検索を行う。 40 public function companysearch(Request $request) 41 { 42 global $meishilist; 43 $param = "$request->companysearch"; 44 $companyresult = Meishi::where('company', 'like', "%{$param}%")->simplePaginate(50); 45 $meishilist = $companyresult; 46 return view('list')->with('meishilist', $meishilist); 47 } 48 49 //名刺情報のうち「お名前」「会社情報」以外で検索を行う「その他検索」を行う。 50 public function othersearch(Request $request) 51 { 52 global $meishilist; 53 $param = "$request->othersearch"; 54 $otherresult = Meishi::where(DB::raw("concat(tel,address,email)"), 'LIKE', "%{$param}%")->simplePaginate(50); 55 $meishilist = $otherresult; 56 return view('list')->with('meishilist', $meishilist); 57 } 58 59 //CSVダウンロードボタンが押された場合 60 public function csvdownload($meishilist) 61 { 62 $array = $meishilist; 63 64 /*$array = array( 65 array( 66 'id' => 1, 67 'name' => '山田太郎', 68 'furigana' => 'やまだたろう', 69 'email' => 'taroyamada@sample.com' 70 ), 71 array( 72 'id' => 3, 73 'name' => '加藤明美', 74 'furigana' => 'かとうあけみ', 75 'email' => 'akemikato@sample.com' 76 ), 77 array( 78 'id' => 5, 79 'name' => '佐藤健夫', 80 'furigana' => 'さとうたけお', 81 'email' => 'takeosato@sample.com' 82 ) 83 ); 84 var_dump($array);*/ 85 86 $file = new SplFileObject('test.csv','w'); 87 foreach ((array)$array as $line) { 88 $file->fputcsv($line); 89 } 90 91 $filepath = 'test.csv'; 92 header('Content-Type: application/octet-stream'); 93 header('Content-Length: '.filesize($filepath)); 94 header('Content-Disposition: attachment; filename=test.csv'); 95 96 readfile($filepath); 97 98 return response()->download('test.csv'); 99 100 } 101 102
ビュー側のソースコードは以下の通りです。
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title>名刺管理システム</title> 6 <style> 7 .search-box{ 8 text-align:right; 9 position:relative;bottom:180px; 10 width:50px; 11 padding:50px; 12 } 13 .csv-download{ 14 position:relative;bottom:50px;left:170px; 15 } 16 .pegenation{ 17 text-align:center; 18 } 19 20 </style> 21 22</head> 23<body> 24<h1>名刺管理システム</h1> 25<p>この画面から新規の名刺情報登録、名刺情報のCSVファイルダウンロード、名刺情報の検索ができます。</p> 26<dev > 27<form action="list" method = "post"> 28 {{ csrf_field() }} 29 <label for="new field">名刺情報の新規登録 </label><br> 30 <input type="submit" name = "newregister" value="新規登録"> 31</form> 32</dev > 33<dev class="csv-download"> 34 <form action="list" method ="post"> 35 {{ csrf_field() }} 36 <label for="new field">名刺情報のCSVダウンロード </label><br> 37 <input type="submit" name = "csvdownload" value="CSVダウンロード"> 38 </form> 39</dev> 40<dev class="search-box"> 41 <p>名刺情報検索</p> 42 43 <form action="list" method="post"> 44 {{csrf_field()}} 45 <input type="text" name = "namesearch" value="{{old('namesearch')}}"> 46 <input type="submit" value="名前検索"> 47 </form> 48 <form action="list" method="post"> 49 {{csrf_field()}} 50 <input type="text" name = "companysearch" value="{{old('companysearch')}}"> 51 <input type="submit" value="会社検索"> 52 </form> 53 <form action="list" method="post"> 54 {{csrf_field()}} 55 <input type="text" name = "othersearch" value="{{old('othersearch')}}"> 56 <input type="submit" value="その他検索"> 57 </form> 58</dev> 59 60<dev class="meishitable"> 61<table border="1" align="center" style="position:relative;bottom:100px;"> 62 <caption>名刺情報リスト</caption> 63 <tr> 64 <th width="300">お名前</th> 65 <th width="300">会社名</th> 66 <th width="300">電話番号</th> 67 <th width="300">住所</th> 68 <th width="300">email</th> 69 </tr> 70 @foreach($meishilist as $meishihyouzi) 71 <tr> 72 <td>{{$meishihyouzi->name}}</td> 73 <td>{{$meishihyouzi->company}}</td> 74 <td>{{$meishihyouzi->tel}}</td> 75 <td>{{$meishihyouzi->address}}</td> 76 <td>{{$meishihyouzi->email}}</td> 77 </tr> 78 @endforeach 79</table> 80 81</dev> 82<dev class="pegenation"> 83 {{$meishilist->links()}} 84</dev> 85</body> 86</html
どうすればいいのかご教授いただけると幸いです。