Skip to content

Commit 398d8f0

Browse files
committed
Readme updated
1 parent ac28fbf commit 398d8f0

File tree

1 file changed

+173
-5
lines changed

1 file changed

+173
-5
lines changed

README.md

Lines changed: 173 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66

77
Eloquent ORM support for ViewComponents
88

9+
10+
## Table of Contents
11+
- [Requirements](#requirements)
12+
- [Installation](#installation)
13+
- [Usage](#usage)
14+
- [Contributing](#contributing)
15+
- [Testing](#testing)
16+
- [Security](#security)
17+
- [License](#license)
18+
19+
20+
## Requirements
21+
22+
* PHP 5.5+ (hhvm & php7 are supported)
23+
24+
925
## Installation
1026

1127
The recommended way of installing the component is through [Composer](https://getcomposer.org).
@@ -16,18 +32,170 @@ Run following command:
1632
composer require view-components/eloquent-data-processing
1733
```
1834

19-
## Security
35+
## Usage
36+
37+
### Creating Data Provider
38+
39+
EloquentDataProvider supports 3 types of data sources:
40+
41+
- Illuminate\Database\Eloquent\Builder instance (database query builder created from model)
42+
- Illuminate\Database\Query\Builder instance (standard database query builder, don't know about models)
43+
- Class name of Eloquent model
44+
45+
#### Using Class Name of Eloquent Model as Data Source
46+
47+
```php
48+
use MyApp\UserModel;
49+
use ViewComponents\Eloquent\EloquentDataProvider;
50+
$provider = new EloquentDataProvider(UserModel::class);
51+
```
52+
53+
If you use class name of Eloquent model as data source,
54+
the only way to modify database query is specifying data provider operations:
55+
56+
57+
```php
58+
use ViewComponents\ViewComponents\Data\Operation\FilterOperation;
59+
60+
$provider->operations()->add(
61+
new FilterOperation('role', FilterOperation::OPERATOR_EQ, 'Manager')
62+
);
63+
```
64+
#### Using Illuminate\Database\Eloquent\Builder as Data Source
65+
66+
```php
67+
use ViewComponents\Eloquent\EloquentDataProvider;
68+
69+
$provider = new EloquentDataProvider((new MyApp\UserModel)->newQuery());
70+
```
71+
72+
It's possible to specify query parts before creating EloquentDataProvider
73+
but note that some parts of query may be changed by data provider operations.
74+
75+
```php
76+
use ViewComponents\Eloquent\EloquentDataProvider;
77+
78+
$query = MyApp\UserModel
79+
::where('role', '=', 'Manager')
80+
->where('company', '=', 'Facebook')
81+
->orderBy('id');
82+
83+
$provider = new EloquentDataProvider($query);
84+
```
85+
86+
#### Using Illuminate\Database\Query\Builder as Data Source
87+
88+
It's possible to use EloquentDataProvider if you not deal with Eloquent models.
89+
90+
```php
91+
use DB;
92+
use ViewComponents\Eloquent\EloquentDataProvider;
93+
94+
$provider = new EloquentDataProvider(
95+
DB::table('users')->where('name', '=', 'David')
96+
);
97+
```
98+
99+
### Data Provider Operations
100+
101+
Eloquent Data provider modifies database query when it has operations.
102+
103+
Use operations() method for accessing operations collection.
104+
105+
Documentation related to collections can be found [here](https://github.com/Nayjest/Collection).
106+
107+
Example of adding operation:
108+
109+
```php
110+
$provider
111+
->operations()
112+
->add(new SortOperation('id', SortOperation::ASC));
113+
114+
```
115+
116+
Also operations can be specified on data provider creation:
117+
118+
```php
119+
120+
use MyApp\UserModel;
121+
use ViewComponents\Eloquent\EloquentDataProvider;
122+
use ViewComponents\ViewComponents\Data\Operation\FilterOperation;
123+
124+
$provider = new EloquentDataProvider(
125+
UserModel::class
126+
[
127+
new FilterOperation('role', FilterOperation::OPERATOR_EQ, 'Manager')
128+
new SortOperation('id', SortOperation::DESC),
129+
]
130+
);
131+
```
132+
133+
### Extracting data
134+
135+
Data providers implements IteratorAggregate interface, so you can iterate it like array:
136+
```php
137+
138+
use MyApp\UserModel;
139+
use ViewComponents\Eloquent\EloquentDataProvider;
140+
141+
$provider = new EloquentDataProvider(UserModel::class);
142+
foreach ($provider as $user) {
143+
var_dump($user); // instance of UserModel
144+
}
145+
146+
```
147+
Data provider executes DB query when getIterator() method is called or when iteration begins in case if data is not loaded yet,
148+
i. e. calling getIterator() twice will not produce 2 database queries.
149+
But changing operations collection will cause resetting cache:
150+
151+
```php
152+
153+
use MyApp\UserModel;
154+
use ViewComponents\Eloquent\EloquentDataProvider;
155+
use ViewComponents\ViewComponents\Data\Operation\FilterOperation;
156+
157+
$provider = new EloquentDataProvider(UserModel::class);
158+
// databse query will be executed
159+
$provider->getIterator();
160+
161+
// databse query will not be executed again, iterating over same data
162+
$provider->getIterator();
163+
164+
$provider->operations->add(
165+
new FilterOperation('id', FilterOperation::OPERATOR_LTE, 5)
166+
)
167+
// databse query will be executed again
168+
$provider->getIterator();
169+
170+
```
171+
172+
173+
## Contributing
174+
175+
Please see [Contributing Guidelines](contributing.md) and [Code of Conduct](code_of_conduct.md) for details.
20176

21-
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
22177

23178
## Testing
24179

25-
Execute phpunit from package folder.
180+
This package bundled with unit tests (PHPUnit).
181+
182+
To run tests locally, you must install this package as stand-alone project with dev-dependencies:
183+
184+
```bash
185+
composer create-project view-components/eloquent-data-processing
186+
```
187+
188+
Command for running tests:
26189

27190
```bash
28-
phpunit
191+
composer test
29192
```
30-
Package dependencies must be installed via composer (run 'composer install').
193+
194+
195+
## Security
196+
197+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
198+
31199

32200
## License
33201

0 commit comments

Comments
 (0)