@@ -10,48 +10,83 @@ A fast and tiny PHP library to export data to CSV and ExcelSheet. The library is
1010composer require sujan/php-csv-exporter
1111```
1212
13- ## Usage
14- All you have to do is to pass the ** Query Builder**
13+ ## Basic Usage
14+ ``` $xslt
15+ $columns = [ 'id', 'name', 'email' ];
1516
17+ $queryBuilder = User::limit(10); // Query Builder
1618
17- `use Sujan\Exporter\Exporter;
18- `
19- ``` $xslt
20- $columns = [
21- 'id',
22- 'name',
23- 'email'
24- ];
25- ```
26- ###### From Query Builder (RECOMMENDED)
19+ $exporter = new Exporter();
20+ $exporter->build($queryBuilder, $columns, 'users.csv')
21+ ->export();
2722```
28- $queryBuilder = User::query(); // Query builder
29- // $queryBuilder = User::where('email', '[email protected] '); // Query builder 3023
31- Exporter::init($queryBuilder, $columns, 'users.csv');
32- // Exporter::init($queryBuilder, $columns, 'users.xlsx'); // For ExcelSheet export
33- Exporter::export();
34- ```
24+ Build and export, that much simple.
25+
26+ ## Documentation
3527
36- ###### From Collection
28+ - [ Build CSV] ( #build-csv )
29+ - [ Export CSV] ( #export-csv )
30+ - [ Usage Examples] ( #usage-examples )
31+ - [ Laravel] ( #laravel )
32+ - [ From Eloquent Query Builder (RECOMMENDED)] ( #from-eloquent-query-builder-recommended )
33+ - [ From Collection] ( #from-collection )
34+ - [ From Array] ( #from-array )
35+ - [ Eloquent Relation] ( #eloquent-relation )
36+ - [ RAW PHP] ( #raw-php )
37+ - [ From Array] ( #from-plain-array )
38+ - [ From PDOStatement (RECOMMENDED)] ( #from-pdostatement-recommended )
39+
40+ ### Build CSV
41+ CSV build takes three parameters. First one is the model which could be ` Array ` , ` PDOStatement ` , ` Eloquent Query Builder ` and
42+ ` Collection ` , seconds one takes the field names you want to export, third one is CSV filename.
43+
44+ ``` $xslt
45+ $exporter->build($queryBuilder, $columns, 'users.csv');
3746```
38- $usersCollection = User::all(); // Collection
3947
40- Exporter::init($usersCollection, $columns, 'users.csv');
41- Exporter::export();
48+ ### Export CSV
49+ ``` $xslt
50+ $exporter->export();
4251```
4352
44- ###### From Array
53+ ## Usage Examples
54+ ### Laravel
55+ You can export data from ` Eloquent Query Builder ` , ` Collection ` and ` Array ` whereas ` Eloquent Query Builder ` is highly recommended.
56+ #### From Eloquent Query Builder (RECOMMENDED)
57+ ``` $xslt
58+ $columns = [ 'id', 'name', 'email' ];
59+
60+ $queryBuilder = User::latest()->whereNotNull('email_verified_at'); // Query Builder
61+
62+ $exporter = new Exporter();
63+ $exporter->build($queryBuilder, $columns, 'users.csv')
64+ ->export();
4565```
46- $usersArray = User::latest()->get()->toArray(); // Array
4766
48- Exporter::init($usersArray, $columns, 'users.csv');
49- Exporter::export();
67+ #### From Collection
68+ ``` $xslt
69+ $columns = [ 'id', 'name', 'email' ];
70+
71+ $collection = User::latest()->get(); // Collection
72+
73+ $exporter = new Exporter();
74+ $exporter->build($collection, $columns, 'users.csv')
75+ ->export();
5076```
5177
52- Or you can pass ` Collection ` or ` Array ` . But it is ** highly recommended** to pass the ** ` Query Builder ` ** as it will use generator to save memory usage.
78+ #### From Array
79+ ``` $xslt
80+ $columns = [ 'id', 'name', 'email' ];
81+
82+ $usersArray = User::latest()->get()->toArray(); // Array of Users
83+
84+ $exporter = new Exporter();
85+ $exporter->build($usersArray, $columns, 'users.csv')
86+ ->export();
87+ ```
5388
54- ** For eloquent relation **
89+ #### Eloquent Relation
5590``` $xslt
5691$columns = [
5792 'id',
@@ -63,20 +98,29 @@ $columns = [
6398
6499$queryBuilder = Post::with('user'); // Query builder
65100
66- Exporter::init($queryBuilder, $columns, 'users.csv');
67- Exporter::export();
101+ $exporter = new Exporter();
102+ $exporter->build($queryBuilder, $columns, 'users.csv')
103+ ->export();
68104```
69105
70- Where ` user ` is the relation name, which is same as is in the ` $columns ` variable.
106+ ### Raw PHP
107+ The library supports Laravel as well as raw PHP. You can easily export data from ` PDOStatement ` and ` Array ` .
71108
72- ## Usage with raw PHP (PDOStatement)
73-
74- Usage with PDO is straight forward. You ** MUST** have to add the following code
109+ #### From Plain Array
75110``` $xslt
76- $stmt->setFetchMode(PDO::FETCH_ASSOC); // N.B. must be included
111+ $array = [
112+ ['id' => 1, 'name' => 'John Doe', 'email' => '[email protected] '], 113+ ['id' => 2, 'name' => 'Jane Doe', 'email' => '[email protected] '] 114+ ];
115+
116+ $columns = ['id', 'name', 'email'];
117+
118+ $exporter = new Exporter();
119+ $exporter->build($array, $columns, 'users.csv')
120+ ->export();
77121```
78122
79- And then pass the ` $stmt ` to ` Exporter `
123+ #### From PDOStatement (RECOMMENDED)
80124``` $xslt
81125 $servername = "localhost";
82126 $username = "root";
@@ -98,19 +142,14 @@ And then pass the `$stmt` to `Exporter`
98142 // set the resulting array to associative
99143 $stmt->setFetchMode(PDO::FETCH_ASSOC);
100144
101- Exporter::init($stmt, $userData, 'users.csv');
102- Exporter::export();
145+ $exporter = new Exporter();
146+ $exporter->build($stmt, $columns, 'users.csv)
147+ ->export();
103148 }
104149 catch(PDOException $e) {
105150 echo "Error: " . $e->getMessage();
106151 }
107152 $conn = null;
108153```
109154
110- ## XLSX export
111- Just use the file extension as ` .xlsx `
112- ```
113- Exporter::init($queryBuilder, $columns, 'users.xlsx');
114- ```
115-
116155## You are always welcome to contribute
0 commit comments