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
Copy file name to clipboardExpand all lines: README.md
+35-15Lines changed: 35 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ public function auditors() {
44
44
[
45
45
'created_by' => 'creator',
46
46
'updated_by' => 'updater',
47
-
'deleted_by' => 'deleter'
47
+
'deleted_by' => 'deleter',
48
48
]
49
49
);
50
50
}
@@ -64,15 +64,28 @@ public function auditors() {
64
64
$post = Post::with('auditors')->first();
65
65
66
66
// Get the creator
67
-
$post->creator;
67
+
$post->auditors->creator;
68
68
69
69
// Get the updater
70
-
$post->updater;
70
+
$post->auditors->updater;
71
71
72
72
// Get the deleter
73
-
$post->deleter;
73
+
$post->auditors->deleter;
74
74
75
75
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
+
76
89
```
77
90
78
91
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{
96
109
[
97
110
'created_by' => 'created',
98
111
'updated_by' => 'updated',
99
-
'deleted_by' => 'deleted'
112
+
'deleted_by' => 'deleted',
100
113
],
101
114
'id'
102
115
);
@@ -112,21 +125,34 @@ To retrieve the audited posts of a user, you can use the audited relationship. H
112
125
$user = User::with('audited')->first();
113
126
114
127
// Get posts created by the user
115
-
$user->created;
128
+
$user->audited->created;
116
129
117
130
// Get posts updated by the user
118
-
$user->updated;
131
+
$user->audited->updated;
119
132
120
133
// 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;
122
148
123
149
```
124
150
125
151
This allows you to define multiple relationships between models with a single method call, simplifying your code and reducing the number of queries executed.
126
152
127
153
### HasManyArrayColumn
128
154
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:
130
156
131
157
```php
132
158
@@ -203,12 +229,6 @@ $company->companyFounders;
203
229
204
230
This will provide you with data from the `users` table where the `founders` array column contains the value 71.
205
231
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.
0 commit comments