Skip to content

Commit 6518ffa

Browse files
authored
Merge pull request #977 from kenjis/docs-adding_attributes_to_users
docs: add explanations to "Adding Attributes to Users"
2 parents d1d1f96 + 550414b commit 6518ffa

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

docs/customization/adding_attributes_to_users.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ php spark db:table users
7474

7575
See [Customizing User Provider](./user_provider.md).
7676

77+
Don't forget to add the added attributes to the `$allowedFields` property.
78+
7779
## Update Validation Rules
7880

7981
You need to update the [validation rules](./validation_rules.md) for registration.

docs/customization/user_provider.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Customizing User Provider
22

3+
## Creating Your Own UserModel
4+
35
If you want to customize user attributes, you need to create your own
46
[User Provider](../getting_started/concepts.md#user-providers) class.
57
The only requirement is that your new class MUST extend the provided `CodeIgniter\Shield\Models\UserModel`.
@@ -13,8 +15,42 @@ php spark shield:model UserModel
1315

1416
The class name is optional. If none is provided, the generated class name would be `UserModel`.
1517

16-
After creating the class, set the `$userProvider` property in **app/Config/Auth.php** as follows:
18+
## Configuring to Use Your UserModel
19+
20+
After creating the class, set your model classname to the `$userProvider` property
21+
in **app/Config/Auth.php**:
1722

1823
```php
1924
public string $userProvider = \App\Models\UserModel::class;
2025
```
26+
27+
## Customizing Your UserModel
28+
29+
Customize your model as you like.
30+
31+
If you add attributes, don't forget to add the attributes to the `$allowedFields`
32+
property.
33+
34+
```php
35+
<?php
36+
37+
declare(strict_types=1);
38+
39+
namespace App\Models;
40+
41+
use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;
42+
43+
class UserModel extends ShieldUserModel
44+
{
45+
protected function initialize(): void
46+
{
47+
parent::initialize();
48+
49+
$this->allowedFields = [
50+
...$this->allowedFields,
51+
'first_name', // Added
52+
'last_name', // Added
53+
];
54+
}
55+
}
56+
```

0 commit comments

Comments
 (0)