Skip to content

Commit f145abd

Browse files
committed
README.md updated
1 parent 136e984 commit f145abd

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

README.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function auditors() {
4444
[
4545
'created_by' => 'creator',
4646
'updated_by' => 'updater',
47-
'deleted_by' => 'deleter'
47+
'deleted_by' => 'deleter',
4848
]
4949
);
5050
}
@@ -64,15 +64,28 @@ public function auditors() {
6464
$post = Post::with('auditors')->first();
6565

6666
// Get the creator
67-
$post->creator;
67+
$post->auditors->creator;
6868

6969
// Get the updater
70-
$post->updater;
70+
$post->auditors->updater;
7171

7272
// Get the deleter
73-
$post->deleter;
73+
$post->auditors->deleter;
7474

7575

76+
// also works with lazy loading
77+
78+
$post = Post::find(7);
79+
80+
// Get the creator
81+
$post->auditors->creator;
82+
83+
// Get the updater
84+
$post->auditors->updater;
85+
86+
// Get the deleter
87+
$post->auditors->deleter;
88+
7689
```
7790

7891
This allows you to define multiple relationships with just one method, and only a single query is fired in the database for all the relationships.
@@ -96,7 +109,7 @@ class User extends Model{
96109
[
97110
'created_by' => 'created',
98111
'updated_by' => 'updated',
99-
'deleted_by' => 'deleted'
112+
'deleted_by' => 'deleted',
100113
],
101114
'id'
102115
);
@@ -112,21 +125,34 @@ To retrieve the audited posts of a user, you can use the audited relationship. H
112125
$user = User::with('audited')->first();
113126

114127
// Get posts created by the user
115-
$user->created;
128+
$user->audited->created;
116129

117130
// Get posts updated by the user
118-
$user->updated;
131+
$user->audited->updated;
119132

120133
// Get posts deleted by the user
121-
$user->deleted;
134+
$user->audited->deleted;
135+
136+
// also works with lazy loading
137+
138+
$user = User::find(71);
139+
140+
// Get posts created by the user
141+
$user->audited->created;
142+
143+
// Get posts updated by the user
144+
$user->audited->updated;
145+
146+
// Get posts deleted by the user
147+
$user->audited->deleted;
122148

123149
```
124150

125151
This allows you to define multiple relationships between models with a single method call, simplifying your code and reducing the number of queries executed.
126152

127153
### HasManyArrayColumn
128154

129-
If you have a column companies in your users table which stores an array of local keys like [7, 71], you can use the following relationship:
155+
If you have a column companies in your users table which stores an array of local keys like [7, 71] or ["7", "71"], you can use the following relationship:
130156

131157
```php
132158

@@ -203,12 +229,6 @@ $company->companyFounders;
203229

204230
This will provide you with data from the `users` table where the `founders` array column contains the value 71.
205231

206-
## Note:
207-
208-
Right now, the `BelongsToManyKeys` and `HasManyKeys` methods work well with eager loading of the relation. However, when loading relations of a single model, the data may not be sorted as expected (e.g., in the order of "updater", "creator", etc.). Instead, all data will be returned as auditors. This functionality will be added in future updates.
209-
210-
While `BelongsToArrayColumn` may work well with data such as `[7,71]`, it may not function properly if the data in the database is `["7","71"]`. It is possible that this issue will be addressed in future updates.
211-
212232
## Testing
213233

214234
```bash

0 commit comments

Comments
 (0)