PHP code example of okamok / csv-download-library

1. Go to this page and download the library: Download okamok/csv-download-library 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/ */

    

okamok / csv-download-library example snippets

 php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Okamok\CsvDownloadLibrary\CsvCreate;    //csv-download-library
use Illuminate\Support\Facades\Response;

class TopController extends Controller
{
    public function index(Request $request)
    {
        //sample header
        $csvHeader = ['title1','title2','title3'];

        //sample data(All users data)
        $data = User::get()->toArray();
        //you also can use Array for data
        //$data = [['aaa','bbb','ccc'], ['ddd','eeee','ffff']];

        //sample file name
        $fileName = 'test.csv';

        //use this library
        $ret = CsvCreate::getCsvData($data, $csvHeader, $fileName, 'UTF-8');

        //CSV output
        return Response::make($ret['csv'], 200, $ret['headers']);
    }
}