Skip to content

Commit 27eb5e4

Browse files
author
Backstage
committed
feat: media optimizations (#7)
1 parent dab0e74 commit 27eb5e4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,33 @@ To register your own fields, you can add them to the `fields.fields` config arra
478478
'custom_fields' => [
479479
App\Fields\CustomField::class,
480480
],
481+
482+
### Value Hydration
483+
484+
The `hydrate` method allows you to transform the raw value stored in the database into a runtime representation. This is useful when you want to convert stored IDs into models, format dates, or process JSON data into specific objects.
485+
486+
To use this feature, your field class must implement the `Backstage\Fields\Contracts\HydratesValues` interface.
487+
488+
```php
489+
use Backstage\Fields\Fields\Base;
490+
use Backstage\Fields\Contracts\HydratesValues;
491+
use Illuminate\Database\Eloquent\Model;
492+
493+
class MyCustomField extends Base implements HydratesValues
494+
{
495+
/**
496+
* Hydrate the raw field value into its runtime representation.
497+
*/
498+
public function hydrate(mixed $value, ?Model $model = null): mixed
499+
{
500+
// Transform the raw value
501+
// For example, convert a stored ID to a model instance
502+
return MyModel::find($value);
503+
}
504+
}
505+
```
506+
507+
The `hydrate` method is automatically called when accessing the value of the field.
481508
```
482509
483510
## Documentation

src/Contracts/HydratesValues.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Backstage\Fields\Contracts;
4+
5+
interface HydratesValues
6+
{
7+
/**
8+
* Hydrate the raw field value into its runtime representation.
9+
*/
10+
public function hydrate(mixed $value, ?\Illuminate\Database\Eloquent\Model $model = null): mixed;
11+
}

0 commit comments

Comments
 (0)