File tree Expand file tree Collapse file tree 2 files changed +54
-4
lines changed Expand file tree Collapse file tree 2 files changed +54
-4
lines changed Original file line number Diff line number Diff line change 2
2
3
3
All notable changes to ` laravel-enum ` will be documented in this file:
4
4
5
- ## 1.0.0 - 2021-03-05
5
+ ## 1.0.0 - 2021-03-08
6
6
7
7
- initial release
Original file line number Diff line number Diff line change 4
4
![ Tests] ( https://github.com/desmart/laravel-enum/workflows/Run%20Tests/badge.svg )
5
5
[ ![ Software License] ( https://img.shields.io/badge/license-MIT-brightgreen.svg )] ( https://github.com/DeSmart/laravel-enum/blob/master/LICENSE )
6
6
7
- Package provides a simple trait that allows to use/generate UUIDs as IDs in Laravel models.
7
+ Package provides a simple way to use strongly typed enum objects with Laravel models. It utilizes Laravel's custom
8
+ casting mechanism.
8
9
9
10
## Installation
10
11
To install the package via Composer, simply run the following command:
11
- ```
12
- composer require desmart/laravel-uuid-id
12
+
13
+ ``` bash
14
+ composer require desmart/laravel-enum
13
15
```
14
16
15
17
## Usage
16
18
19
+ Create an enum class that extends ` DeSmart\Laravel\Enumeration ` . Then, simply define all possible values in form of
20
+ class constants:
21
+
22
+ ``` php
23
+ class Character extends DeSmart\Laravel\Enumeration
24
+ {
25
+ const GOOD = 'good';
26
+ const EVIL = 'evil';
27
+ const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil';
28
+ }
29
+ ```
30
+
31
+ In Laravel model:
32
+
33
+ ``` php
34
+ class Hero extends Model
35
+ {
36
+ /**
37
+ * @var array
38
+ */
39
+ protected $casts = [
40
+ 'character' => Character::class,
41
+ ];
42
+ }
43
+ ```
44
+
45
+ That's it.
46
+
47
+ ``` php
48
+ $hero = new Hero(['character' => Character::EVIL]);
49
+
50
+ dump($hero);
51
+ // Hero {#293
52
+ // ...
53
+ // #casts: array:1 [
54
+ // "character" => "Character"
55
+ // ]
56
+ // ...
57
+ // #attributes: array:1 [
58
+ // "character" => "evil"
59
+ // ]
60
+ // }
61
+
62
+ dump($hero->character);
63
+ // Character {#296
64
+ // -value: "good"
65
+ // }
66
+ ```
17
67
## Changelog
18
68
19
69
Please see [ CHANGELOG] ( CHANGELOG.md ) for more information what has changed recently.
You can’t perform that action at this time.
0 commit comments