Skip to content

Commit 45e2e7a

Browse files
committed
doc updated for using with raw php
1 parent b1cef5b commit 45e2e7a

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ $columns = [
1616
'email'
1717
];
1818
19-
$exporter = new Export(
20-
User::query(),
21-
$columns
22-
);
19+
$users = User::query(); // Query builder
2320
24-
$exporter->export();
21+
Exporter::init($users, $columns, 'users.csv');
22+
Exporter::export();
2523
```
2624

25+
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.
26+
2727
**For eloquent relation**
2828
```$xslt
2929
$columns = [
@@ -43,3 +43,43 @@ $exporter->export();
4343
```
4444

4545
Where `user` is the relation name, which is same as is in the `$columns` variable.
46+
47+
## Usage with raw PHP (PDOStatement)
48+
49+
Usage with PDO is straight forward. You **MUST** have to add the following code
50+
```$xslt
51+
$stmt->setFetchMode(PDO::FETCH_ASSOC); // N.B. must be included
52+
```
53+
54+
And then pass the `$stmt` to `Exporter`
55+
```$xslt
56+
$servername = "localhost";
57+
$username = "root";
58+
$password = "password";
59+
$dbname = "laravel";
60+
61+
$columns = [
62+
'id',
63+
'name',
64+
'email'
65+
];
66+
67+
try {
68+
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
69+
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
70+
$stmt = $conn->prepare("SELECT id, name, email FROM users");
71+
$stmt->execute();
72+
73+
// set the resulting array to associative
74+
$stmt->setFetchMode(PDO::FETCH_ASSOC);
75+
76+
Exporter::init($stmt, $userData, 'users.csv');
77+
Exporter::export();
78+
}
79+
catch(PDOException $e) {
80+
echo "Error: " . $e->getMessage();
81+
}
82+
$conn = null;
83+
```
84+
85+
## You are always welcome to contribute

0 commit comments

Comments
 (0)