You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: fields.md
+29-57Lines changed: 29 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,19 +33,6 @@ public function fields(Request $request): array
33
33
}
34
34
```
35
35
36
-
Alternatively, you can use `withFields` method on an object that resolves actions. It can be useful when you just want to hook into the object for some reason.
37
-
38
-
```php
39
-
use App\Root\Fields\Text;
40
-
use Illuminate\Http\Request;
41
-
42
-
$resource->withFields(static function (Request $request): array {
43
-
return [
44
-
Text::make(__('Title'), 'title'),
45
-
];
46
-
});
47
-
```
48
-
49
36
### Configuration
50
37
51
38
### Value Resolution
@@ -85,25 +72,18 @@ Number::make('Price')
85
72
86
73
### Value Hydration
87
74
88
-
You may define custom value hydration logic on your field class. To do so, you can easily override the default `hydrate` method:
75
+
You may define custom value hydration logic on your field. To do so, use the `hydrate` method passing a `Closure`:
89
76
90
77
```php
91
-
namespace App\Root\Fields;
92
-
93
-
use Cone\Root\Fields\Field;
94
-
use Illuminate\Http\Request;
78
+
use Cone\Root\Fields\Number;
95
79
use Illuminate\Database\Eloquent\Model;
80
+
use Illuminate\Http\Request;
81
+
use Illuminate\Support\Number;
96
82
97
-
class CustomField extends Field
98
-
{
99
-
/**
100
-
* Hydrate the model.
101
-
*/
102
-
public function hydrate(Request $request, Model $model, mixed $value): void
103
-
{
104
-
$model->setAttribute($this->name, $value);
105
-
}
106
-
}
83
+
Number::make('Price')
84
+
->hydrate(static function (Request $request, Model $model, mixed $value): void {
85
+
$model->setAttribute('price', $value);
86
+
});
107
87
```
108
88
109
89
## Authorization
@@ -113,38 +93,11 @@ You may allow/disallow interaction with fields. To do so, you can call the `auth
113
93
```php
114
94
use Illuminate\Http\Request;
115
95
116
-
$field->authorize(static function (Request $request): bool {
$field->authorize(static function (Request $request, Model $model): bool {
97
+
return $request->user()->isAdmin();
143
98
});
144
99
```
145
100
146
-
> The `$model` and the `$related` parameters can be null, since they are only passed to the callback when they are available in the current authorization context.
147
-
148
101
### Visibility
149
102
150
103
You may show or hide fields based on the current resource view. For example, some fields might be visible on the `index` page, while others should be hidden. You can easily customize the action visibility logic using the `visibleOn` and `hiddenOn` methods:
@@ -191,8 +144,27 @@ $field->searchable(false);
191
144
192
145
### Boolean
193
146
147
+
The `Boolean` field is typically a handler for `boolean` model attributes:
148
+
149
+
> Don't forget to [cast](https://laravel.com/docs/master/eloquent-mutators#attribute-casting) you model attribute as a `boolean`.
150
+
151
+
```php
152
+
$field = Boolean::make(__('Enabled'), 'enabled');
153
+
```
154
+
194
155
### Checkbox
195
156
157
+
The `Checkbox` field is typically a handler for (array of values) `json` model attributes:
158
+
159
+
> Don't forget to [cast](https://laravel.com/docs/master/eloquent-mutators#attribute-casting) you model attribute as a `json` or `array`.
0 commit comments