PHP code example of stephen / chunk-export

1. Go to this page and download the library: Download stephen/chunk-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stephen / chunk-export example snippets


 

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Stephen\Chunk\Service\ChunkService;

class Export extends Controller
{
    //
    /**
     * @var ChunkService
     */
    private $chunkService;
    /**
     * @var User
     */
    private $user;

    /**
     * Export constructor.
     * @param ChunkService $chunkService
     */
    public function __construct(ChunkService $chunkService, User $user)
    {
        $this->chunkService = $chunkService;
        $this->user = $user;
    }

    public function index()
    {
        $config['models'] = $this->user->select('id', 'name', 'email')->orderBy('id', 'DESC');
        $config['headers'] = ['姓名', '邮箱'];
        $config['filename'] = '用户信息.csv';
        $pageSize = 10;

        $callback = function ($user) {
            $output = [];

            $output['name'] = iconv('UTF-8', 'GBK', $user['name']);
            $output['email'] = iconv('UTF-8', 'GBK', $user['email']);
            return $output;

        };
        $this->chunkService->exportCsv($config, $callback, $pageSize);


    }
}