Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for Enum #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions docs/en/docs/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,36 @@ Select::make('user')

This will use the `uuid` field as the key for each option, rather than the default primary key.

Enum as source:

```php
namespace App\Casts;

enum Type: string
{
case General = 'general';
case Account = 'account';
case SecurityAndPrivacy = 'security-and-privacy';

/**
* @return string
*/
public function title(): string
{
return match ($this) {
Type::General => __('types.general.title'),
Type::Account => __('types.account.title'),
Type::SecurityAndPrivacy => __('types.security-and-privacy.title'),
};
}
}
```

```php
Select::make('type')
->fromEnum(Type::class);
```

If you want to provide an option that represents an unselected state, you can use the `empty()` method:

```php
Expand Down Expand Up @@ -317,6 +347,63 @@ Select::make('type')
]);
```


Enum Casting
```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Type;

class Question extends Model
{

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => Type::class,
];
}
````

Screen
````php
use App\Casts\Type;
use App\Models\Question;

public function query(Question $question): array
{
return [
'question' => $question,
];
}


public function types(): array
{
return collect(Type::cases())
->mapWithKeys( fn ( Type $type ) => [ $type->value => $type->title() ] )
->all();
}
````

Usage:
```php
Select::make('question.type')
->fromEnum(Type::class);
```

Or:

````php
Select::make('question.type')
->options($this->types())
->title('Тип'),
````

## Relation


Expand Down Expand Up @@ -427,6 +514,73 @@ Relation::make('users.')
->title('Select users');
```

Enum Casting
```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Type;

class Section extends Model
{

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => Type::class,
];
}
````

```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Type;

class Question extends Model
{

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => Type::class,
];
}
````

Screen
````php
use App\Casts\Type;
use App\Models\Question;

public function query(Question $question): array
{
return [
'question' => $question,
];
}
````

Usage:
```php
Relation::make('question.section')
->fromModel(Section::class, 'type')

```

```php
Relation::make('question.type')
->fromModel(Section::class, 'type', 'type')

```


## DateTime

Expand Down
153 changes: 152 additions & 1 deletion docs/ru/docs/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,37 @@ Select::make('user')
->fromModel(User::class, 'email', 'uuid');
```

Enum в качестве источника:

```php
namespace App\Casts;

enum Type: string
{
case General = 'general';
case Account = 'account';
case SecurityAndPrivacy = 'security-and-privacy';

/**
* @return string
*/
public function title(): string
{
return match ($this) {
Type::General => __('types.general.title'),
Type::Account => __('types.account.title'),
Type::SecurityAndPrivacy => __('types.security-and-privacy.title'),
};
}
}
```

```php
Select::make('type')
->fromEnum(Type::class);
```


Возможны ситуации, когда необходимо добавить некоторое значение, которое означает, что поле не выбрано.
Для этого можно использовать метод `empty`:

Expand Down Expand Up @@ -270,8 +301,62 @@ Select::make('user')
]);
```

## Relation
Enum Casting
```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Type;

class Question extends Model
{

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => Type::class,
];
}
````

В экране
````php
use App\Casts\Type;
use App\Models\Question;

public function query(Question $question): array
{
return [
'question' => $question,
];
}


public function types(): array
{
return collect(Type::cases())
->mapWithKeys( fn ( Type $type ) => [ $type->value => $type->title() ] )
->all();
}
````
Использование:
```php
Select::make('question.type')
->fromEnum(Type::class);
```

Или

````php
Select::make('question.type')
->options($this->types())
->title('Тип'),
````

## Relation

Поля отношения могут подгружать динамические данные, это хорошее решение, если вам нужны связи.

Expand Down Expand Up @@ -315,6 +400,7 @@ class Idea extends Model
}
```


```php
Relation::make('idea')
->fromModel(Idea::class, 'name')
Expand Down Expand Up @@ -376,6 +462,71 @@ Relation::make('users.')
->title('Select users');
```

Enum Casting
```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Type;

class Section extends Model
{

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => Type::class,
];
}
````
```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Casts\Type;

class Question extends Model
{

/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'type' => Type::class,
];
}
````

В экране
````php
use App\Casts\Type;
use App\Models\Question;

public function query(Question $question): array
{
return [
'question' => $question,
];
}
````

Использование:
```php
Relation::make('question.section')
->fromModel(Section::class, 'type')

```

```php
Relation::make('question.type')
->fromModel(Section::class, 'type', 'type')

```

## DateTime

Expand Down