このページのほぼコピペで
https://programmer-jobs.blogspot.jp/2017/04/laravel-5-4-csv-download.html
TESTで作ったサイトでは動いたのですが
今回作ったサイトでは動かず原因特定が私では難しいのでヘルプして頂きたいです。
たぶん解る人なら10分も掛からない作業かと思います。
テーブル名
customers
controller名
CustomersController
php
1//CustomersController.php 2namespace App\Http\Controllers; 3 4use Illuminate\Http\Request; 5use DB; 6use Carbon\Carbon; 7use \App\User; 8class CustomersController extends Controller 9{ 10 public function __construct() 11 { 12 $this->middleware('auth')->except(['index', 'confirm','complete']); 13 } 14 15//一部アクション省略 16 17 //CSV 18 public function csvDownload() 19 { 20 $headers = array( 21 "Content-type" => "text/csv", 22 "Content-Disposition" => "attachment; filename=file.csv", 23 "Pragma" => "no-cache", 24 "Cache-Control" => "must-revalidate, post-check=0, pre-check=0", 25 "Expires" => "0" 26 ); 27 28 $callback = function() { 29 $handle = fopen('php://output', 'w'); 30 31 $columns = [ 32 'id', 33 'name', 34 'sex', 35 ] ; 36 mb_convert_variables('SJIS-win', 'UTF-8', $columns); 37 38 fputcsv($handle, $columns); 39 40 $customers = Customers::all(); 41 foreach ($customers as $customer) { 42 $csv = [ 43 $customer->id, 44 $customer->name, 45 $customer->sex, 46 ] ; 47 mb_convert_variables('SJIS-win', 'UTF-8', $csv); 48 fputcsv($handle, $csv); 49 } 50 51 fclose($handle); 52 }; 53 return response()->stream($callback, 200, $headers); 54 }
php
1//web.php 2Route::get('/', 'CustomersController@index'); 3Route::post('/','CustomersController@confirm'); 4Route::post('/complete','CustomersController@complete'); 5 6Auth::routes(); 7Route::get('/dl', 'CustomersController@csvDownload'); 8Route::get('/home', 'HomeController@index')->name('home');
php
1//home.blade.php 2@extends('layouts.app') 3 4@section('content') 5<div class="container"> 6 <div class="row"> 7 <div class="col-md-8 col-md-offset-2"> 8 <div class="panel panel-default"> 9 <div class="panel-heading">管理メニュー</div> 10 11 <div class="panel-body"> 12 @if (session('status')) 13 <div class="alert alert-success"> 14 {{ session('status') }} 15 </div> 16 @endif 17 18 <ul> 19 <li><a href="/dl">CsvDownload</a></li> 20 </ul> 21 </div> 22 </div> 23 </div> 24 </div> 25</div> 26@endsection 27 28コード
症状としては、
このサイトにアクセスできません
と出て、DLされない。
エラーログを見ると
$customers = Customers::all();
でエラーが出ている
php
1//エラーログ 2[2017-12-08 16:42:10] local.ERROR: Class 'App\Http\Controllers\Customers' not found {"userId":2,"email":"beruzea@gmail.com","exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'App\Http\Controllers\Customers' not found at /home/kusanagi/laravel/app/Http/Controllers/CustomersController.php:131) 3[stacktrace] 4#0 [internal function]: App\Http\Controllers\CustomersController->App\Http\Controllers\{closure}() 5#1 /home/kusanagi/laravel/vendor/symfony/http-foundation/StreamedResponse.php(114): call_user_func(Object(Closure)) 6#2 /home/kusanagi/laravel/vendor/symfony/http-foundation/Response.php(371): Symfony\Component\HttpFoundation\StreamedResponse->sendContent() 7#3 /home/kusanagi/laravel/DocumentRoot/index.php(58): Symfony\Component\HttpFoundation\Response->send() 8#4 {main} 9"} 10
自分なりにエラーログを見てcontrollerを見失っている???と思い
use App\Customers;を追加してみたり自分なりには、色々と試しては見たのですが
お手上げです。
ご教示お願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/08 07:59
退会済みユーザー
2017/12/08 08:02
2017/12/08 08:05
退会済みユーザー
2017/12/08 08:12
2017/12/08 08:18