1

following is the code which I used for export functionality

public function export(){
        $users = DB::table('users')->where('user_type','Farmer')->orWhere('user_type','Dealer')->select('id','name','middlename','surname','mobile','state','district','taluka','user_type')->get()->map(function ($item, $key){
            return (array) $item;
        })
        ->all();

        $usersArray = []; 
        foreach ($users as $user) {
             $usersArray[] =$user;
        }


        Excel::create('users',function($excel)use($usersArray) {
            $excel->setTitle('users');
            $excel->setCreator('Laravel');
            $excel->setDescription('users file');
            $excel->sheet('sheet1',function($sheet)use($usersArray) {
            $sheet->fromArray($usersArray,null,'A1',true,true);
            });

        })->download('xlsx');
    }

This code is working fine on localhost,no issue at localhost, But on server it is showing error that "This site can't be reached", I am unable to figure it out that what is the issue on server. I even try some other solutions on stackoverflow but unable to work.

Nil
  • 513
  • 3
  • 16

3 Answers3

1
     $users = DB::table('users')
               ->where('user_type','Farmer')
               ->orWhere('user_type','Dealer')
               ->select('id', 'name', 'middlename', 'surname', 'mobile', 'state', 'district', 'taluka','user_type')
               ->get()->map(function ($item, $key){
                    return (array) $item;
               })->all()->toArray();

After using ->toArray(), the variable $users variable will be an array

Now coming to your point,

Possibilites of Error

1) Folder permission to create a temp file

2) Allowed memory size exceeded, you can change the memory size to unlimited in php.ini file or in your code by setting ini_set('memory_limit','-1');

3) Give write permission to the storage directory of laravel setup in the server.

4) check the apache/nginx log for some other possibilities of error, as it is a 500 error response it should be logged into the log file of apache/nginx.

My suggestion is to use league/csv packages or box/spout packages with laravel Excel related works.

Reiah Paul Sam
  • 555
  • 6
  • 16
  • 1
    I directly used box/spout and it is working fine on both localhost as well as on server, Thanks for the help. – Nil May 15 '17 at 06:45
  • What happened yesterday, by mistake I make testing both the time on localhost and I thought that it was working on server now and even I mark that answer active.. 1.Created php.ini file on server.... 2.set memory limit to -1, i.e. 128M from 64M 3.set wite permission to storage folder But export is still not working... – Nil May 16 '17 at 06:42
  • restarted apache in server ? – Reiah Paul Sam May 16 '17 at 11:16
  • Yes I checked after restaring apache but still unable to work...When I ceck log , I get this log error: production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'ZipArchive' not found' – Nil May 24 '17 at 07:09
  • I search solution for this and try to update php to 7.0 But we are having shared server so unable to update php version to 7.0 – Nil May 24 '17 at 07:10
  • as you are getting Class 'ZipArchive' not found, it seems PHP needs to have the zip extension installed. here is the link, check for the php version and install the zip extension for that version http://www.php.net/manual/en/zip.installation.php – Reiah Paul Sam May 25 '17 at 05:43
  • https://stackoverflow.com/a/41627136/6708612 ..after lot of trouble finally this solution is worked for me. I just append PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP); this line before $zipClass = PHPExcel_Settings::getZipClass(); In File PHPExcel/Reader/Excel2007.php, and Now my export function is running on server too. – Nil May 25 '17 at 07:19
  • @Nil Oh Okay , you are forcing to choose the default ZipClass from the package ? – Reiah Paul Sam May 25 '17 at 07:25
0

Just a hint, you don't have to make $usersArray by own, just use toArray() method after all().

Is there any message in your nginx/apache log?

Jakub Kratina
  • 644
  • 6
  • 14
0

PHPExcel ZipArchive not found after lot of trouble finally this solution is worked for me. I just append PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP); this line before $zipClass = PHPExcel_Settings::getZipClass(); In File PHPExcel/Reader/Excel2007.php, and Now my export function is running on server too.

Nil
  • 513
  • 3
  • 16