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
You may validate that an enum value passed to a controller is a valid value for a given enum by using the `EnumerationValue` rule, for easier handling there are helper methods for creating the rule: `Enumeration::makeRule()`, `Enumeration::makeRuleWithWhitelist($whitelist)` and `Enumeration::makeRuleWithBlacklist($blacklist)`.
174
+
You may validate that a value passed to a controller is a valid value for a given enum by using the `EnumerationValue` rule, for easier handling there are helper methods for creating the rule: `Enumeration::makeRule()`, `Enumeration::makeRuleWithWhitelist($whitelist)` and `Enumeration::makeRuleWithBlacklist($blacklist)`.
165
175
166
176
```php
167
177
public function store(Request $request)
@@ -223,7 +233,6 @@ return [
223
233
```php
224
234
// resources/lang/da/enums.php
225
235
<?php
226
-
227
236
use App\Enums\UserType;
228
237
229
238
return [
@@ -235,6 +244,64 @@ return [
235
244
];
236
245
```
237
246
247
+
## Model Trait
248
+
249
+
You can use the `HasEnums` trait to enable automatic casting of properties to enums. When used, it also automatically checks if a value set to an enum property is valid, if not it throws an `UndefinedMemberException`.
250
+
251
+
To enable this functionality you will have to use the trait and set the enums property on your model.
252
+
253
+
```php
254
+
<?php
255
+
256
+
namespace App\Models;
257
+
258
+
use App\Enums\UserType;
259
+
use Illuminate\Database\Eloquent\Model;
260
+
use Sourceboat\Enumeration\Traits\HasEnums;
261
+
262
+
class User extends Model
263
+
{
264
+
use HasEnums;
265
+
266
+
protected $enums = [
267
+
'type' => UserType::class,
268
+
];
269
+
}
270
+
```
271
+
272
+
The enums property is a simple mapping of model attributes to enum classes. For cases where you want an attribute to be nullable, but don't want to have a `null`-value member in your enum, you can specify this explicitly:
If the casted attribute is not set to be nullable and/or has a value not represented by the enum, you will get the default member of the enum when accessing the attribute.
294
+
295
+
```php
296
+
// For example when the value has been changed manually in the database. Let's say the type is `10`.
Much of the functionality in this Package is inspired by [bensampo/laravel-enum](https://github.com/bensampo/laravel-enum) and some code has been taken from it and modified, for example the `MakeEnumCommand.php`, the `EnumServiceProvider.php` and this readme.
@@ -248,4 +315,4 @@ This package is also licensed under the MIT license.
248
315
## TODOs
249
316
250
317
*[x] Tests for all enumeration-functions and the rule.
251
-
*[] Model-Trait to enable casting of enumerations and "on the fly"-validation for enumeration values on models.
318
+
*[x] Model-Trait to enable casting of enumerations and "on the fly"-validation for enumeration values on models.
0 commit comments