File tree 1 file changed +17
-10
lines changed
1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change @@ -141,25 +141,32 @@ public function addRows(Collection $models)
141
141
}
142
142
143
143
/**
144
- * @param Model $model
145
- * @param string $relation Name of child relation in model
146
- * @param string $alias Name of attribute set with each model
144
+ * @param Model $model
145
+ * @param string|array $relations Name of child relation in model
146
+ * @param string $alias Name of attribute set with each model
147
147
*
148
148
* @return void
149
149
* @throws CannotInsertRecord
150
150
*/
151
- public function addRowForEachRelation (Model $ model , string $ relation , string $ alias )
151
+ public function addRowForEachRelation (Model $ model , $ relations , string $ alias )
152
152
{
153
- if (! data_get ( $ model , $ relation )) {
154
- $ this -> addRow ( $ model ) ;
153
+ $ relations = ! is_array ( $ relations ) ? [ $ relations ] : $ relations ;
154
+ $ hasRelation = false ;
155
155
156
- return ;
156
+ foreach ($ relations as $ relation ) {
157
+ $ relation = data_get ($ model , $ relation );
158
+
159
+ foreach ($ relation as $ relationModel ) {
160
+ $ hasRelation = true ;
161
+ $ model ->$ alias = $ relationModel ;
162
+ $ this ->addRow ($ model );
163
+ unset($ model ->$ alias );
164
+ }
157
165
}
158
166
159
- foreach ( data_get ( $ model , $ relation ) as $ relationModel ) {
160
- $ model -> $ alias = $ relationModel ;
167
+ // has no relations, insert only one row
168
+ if (! $ hasRelation ) {
161
169
$ this ->addRow ($ model );
162
- unset($ model ->$ alias );
163
170
}
164
171
}
165
172
You can’t perform that action at this time.
0 commit comments