Skip to content

Commit 69c15ea

Browse files
committed
wip
1 parent 0f5362e commit 69c15ea

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

docs/docs/3.0/repository-pattern/field.md

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Field
22

3-
Field is basically the model attribute representation. Each Field generally extend the Field class from the Restify.
4-
This class ships a variety of mutators, interceptors, validators chaining methods you can use for defining your attribute
5-
according with your needed.
3+
Field is basically the model attribute representation. Each Field generally extends the `Binaryk\LaravelRestify\Fields\Field` class from the Laravel Restify.
4+
This class ships a variety of mutators, interceptors, validators chaining methods you can use for defining your attribute.
65

76
To add a field to a repository, we can simply add it to the repository's fields method.
8-
Typically, fields may be created using their static make method. This method accepts the underlying database column as
9-
argument:
7+
Typically, fields may be created using their static `new` or `make` method. These methods accept the underlying database column as argument:
108

119
```php
1210

@@ -21,10 +19,10 @@ use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
2119
public function fields(RestifyRequest $request)
2220
{
2321
return [
24-
Field::make('email')->rules('required')->storingRules('unique:users')->messages([
22+
Field::new('email')->rules('required')->storingRules('unique:users')->messages([
2523
'required' => 'This field is required.',
2624
]),
27-
Field::make('password')->storeCallback(function ($value) {
25+
Field::new('password')->storeCallback(function ($value) {
2826
return Hash::make($value);
2927
})->rules('required')->storingRules('confirmed'),
3028
];
@@ -33,31 +31,31 @@ public function fields(RestifyRequest $request)
3331

3432
# Validation
3533

36-
There is a gold rule saying - catch the exception as soon as possible on it's request way.
34+
There is a gold rule saying - catch the exception as soon as possible on its request way.
3735
Validations are the first bridge of your request information, it would be a good start to validate
38-
your input so you don't have to worry about payload anymore.
36+
your input. So you don't have to worry about the payload anymore.
3937

4038
## Attaching rules
4139

42-
Validation rules could be add by chaining the `rules` method to attach [validation rules](https://laravel.com/docs/validation#available-validation-rules)
40+
Validation rules could be adding by chaining the `rules` method to attach [validation rules](https://laravel.com/docs/validation#available-validation-rules)
4341
to the field:
4442

4543
```php
46-
Field::make('email')->rules('required'),
44+
Field::new('email')->rules('required'),
4745
```
4846

4947
Of course, if you are leveraging Laravel's support for [validation rule objects](https://laravel.com/docs/validation#using-rule-objects),
5048
you may attach those to resources as well:
5149

5250
```php
53-
Field::make('email')->rules('required', new CustomRule),
51+
Field::new('email')->rules('required', new CustomRule),
5452
```
5553

5654
Additionally, you may use [custom Closure rules](https://laravel.com/docs/validation#using-closures)
5755
to validate your resource fields:
5856

5957
```php
60-
Field::make('email')->rules('required', function($attribute, $value, $fail) {
58+
Field::new('email')->rules('required', function($attribute, $value, $fail) {
6159
if (strtolower($value) !== $value) {
6260
return $fail('The '.$attribute.' field must be lowercase.');
6361
}
@@ -69,7 +67,7 @@ Field::make('email')->rules('required', function($attribute, $value, $fail) {
6967
If you would like to define rules that only apply when a resource is being storing, you may use the `storingRules` method:
7068

7169
```php
72-
Field::make('email')
70+
Field::new('email')
7371
->rules('required', 'email', 'max:255')
7472
->storingRules('unique:users,email');
7573
```
@@ -79,24 +77,24 @@ Field::make('email')
7977
Likewise, if you would like to define rules that only apply when a resource is being updated, you may use the `updatingRules` method.
8078

8179
```php
82-
Field::make('email')->updatingRules('required', 'email');
80+
Field::new('email')->updatingRules('required', 'email');
8381
```
8482

8583

8684
# Interceptors
87-
However the default storing process is done automatically, sometimes you may want to take the control over it.
85+
However, the default storing process is automatically, sometimes you may want to take the control over it.
8886
That's a breeze with Restify, since Field expose few useful chained helpers for that.
8987

9088
## Fill callback
9189

9290
There are two steps before the value from the request is attached to model attribute.
9391
Firstly it is get from the application request, and go to the `fillCallback` and secondly,
94-
the value is transforming by the `storeCallback`.
92+
the value is transforming by the `storeCallback` or `updateCallback`:
9593

9694
You may intercept each of those with closures.
9795

9896
```php
99-
Field::make('title')
97+
Field::new('title')
10098
->fillCallback(function (RestifyRequest $request, $model, $attribute) {
10199
$model->{$attribute} = strtoupper($request->get('title_from_the_request'));
102100
})
@@ -112,7 +110,52 @@ Another handy interceptor is the `storeCallback`, this is the step immediately b
112110
This interceptor may be useful for modifying the value passed through the `$request`.
113111

114112
```php
115-
Field::make('password')->storeCallback(function ($value) {
116-
return Hash::make($value);
113+
Field::new('password')->storeCallback(function ($value) {
114+
return Hash::new($value);
117115
});
118116
```
117+
118+
## Update callback
119+
120+
```php
121+
Field::new('password')->updateCallback(function ($value) {
122+
return Hash::new($value);
123+
});
124+
```
125+
## Index Callback
126+
127+
Sometimes you may want to transform some attribute from the database right before it is returned to the frontend.
128+
129+
Transform the value for the index request:
130+
131+
```php
132+
Field::new('password')->indexCallback(function ($value) {
133+
return Hash::new($value);
134+
});
135+
```
136+
## Show callback
137+
138+
Transform the value for the show request:
139+
140+
```php
141+
Field::new('password')->showRequest(function ($value) {
142+
return Hash::new($value);
143+
});
144+
```
145+
146+
## Append Callback
147+
148+
Very often there is necessary to store a field as `auth()->user()->id`. This field could not be passed from the
149+
frontend:
150+
151+
```php
152+
Field::new('user_id')->hidden()->append(auth()->user()->id);
153+
```
154+
155+
or using a closure:
156+
157+
```php
158+
Field::new('user_id')->hidden()->append(function(RestifyRequest $request, $model, $attribute) {
159+
return auth()->user()->id;
160+
});
161+
```

src/Repositories/Repository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public function index(RestifyRequest $request)
535535
})->values()->map(fn (self $repository) => $repository->serializeForIndex($request));
536536

537537
return $this->response([
538-
'meta' => $this->resolveIndexMainMeta($request) ?? RepositoryCollection::meta($paginator->toArray()),
538+
'meta' => $this->resolveIndexMainMeta($request, $items) ?? RepositoryCollection::meta($paginator->toArray()),
539539
'links' => RepositoryCollection::paginationLinks($paginator->toArray()),
540540
'data' => $items,
541541
]);
@@ -545,7 +545,7 @@ public function indexPaginator(RestifyRequest $request)
545545
{
546546
}
547547

548-
public function resolveIndexMainMeta(RestifyRequest $request)
548+
public function resolveIndexMainMeta(RestifyRequest $request, Collection $items)
549549
{
550550
//
551551
}

0 commit comments

Comments
 (0)