Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 18, 2024
1 parent 79c4b80 commit 8ac006a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 61 deletions.
3 changes: 0 additions & 3 deletions core-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Product::getProxiedContract();

// Proxied calls to the bound instance
Product::proxy()->newQuery()->where(...);
Product::proxy()->variation(...);

// Dynamic usage of bound classes
public function product()
Expand All @@ -85,8 +84,6 @@ class Addon extends Model implements Contract

/**
* Get the proxied contract.
*
* @return string
*/
public static function getProxiedContract(): string
{
Expand Down
86 changes: 29 additions & 57 deletions fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ public function fields(Request $request): array
}
```

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.

```php
use App\Root\Fields\Text;
use Illuminate\Http\Request;

$resource->withFields(static function (Request $request): array {
return [
Text::make(__('Title'), 'title'),
];
});
```

### Configuration

### Value Resolution
Expand Down Expand Up @@ -85,25 +72,18 @@ Number::make('Price')

### Value Hydration

You may define custom value hydration logic on your field class. To do so, you can easily override the default `hydrate` method:
You may define custom value hydration logic on your field. To do so, use the `hydrate` method passing a `Closure`:

```php
namespace App\Root\Fields;

use Cone\Root\Fields\Field;
use Illuminate\Http\Request;
use Cone\Root\Fields\Number;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Number;

class CustomField extends Field
{
/**
* Hydrate the model.
*/
public function hydrate(Request $request, Model $model, mixed $value): void
{
$model->setAttribute($this->name, $value);
}
}
Number::make('Price')
->hydrate(static function (Request $request, Model $model, mixed $value): void {
$model->setAttribute('price', $value);
});
```

## Authorization
Expand All @@ -113,38 +93,11 @@ You may allow/disallow interaction with fields. To do so, you can call the `auth
```php
use Illuminate\Http\Request;

$field->authorize(static function (Request $request): bool {
return $request->user()->can('create', Post::class);
});
```

Also, when authorizing fields, pivot fields or actions, you may have access to the models of the context:

```php
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

// Action or top level field
$field->authorize(static function (Request $request, ?Model $model = null): bool {
if (is_null($model)) {
return $request->user()->can('create', Post::class);
}

return $request->user()->can('view', $model);
});

// Pivot field
$field->authorize(static function (Request $request, ?Model $model = null, ?Model $related = null): bool {
if (is_null($model) || is_null($related)) {
return $request->user()->can('create', Tag::class);
}

return $request->user()->can('attachTag', $model, $related);
$field->authorize(static function (Request $request, Model $model): bool {
return $request->user()->isAdmin();
});
```

> 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.
### Visibility

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:
Expand Down Expand Up @@ -191,8 +144,27 @@ $field->searchable(false);

### Boolean

The `Boolean` field is typically a handler for `boolean` model attributes:

> Don't forget to [cast](https://laravel.com/docs/master/eloquent-mutators#attribute-casting) you model attribute as a `boolean`.
```php
$field = Boolean::make(__('Enabled'), 'enabled');
```

### Checkbox

The `Checkbox` field is typically a handler for (array of values) `json` model attributes:

> Don't forget to [cast](https://laravel.com/docs/master/eloquent-mutators#attribute-casting) you model attribute as a `json` or `array`.
```php
$field = Checkbox::make(__('Permissions'), 'permissions')
->options([
//
]);
```

### Color

### Date
Expand Down
2 changes: 1 addition & 1 deletion installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Requirements:

- PHP `8.2+`
- PHP `GD` and `EXIF` extensions
- Laravel `^10.0`
- Laravel `^11.0`

## Installation

Expand Down

0 comments on commit 8ac006a

Please sign in to comment.