Skip to content

Commit 9d189fe

Browse files
committed
adding support for more relations
1 parent cf6f312 commit 9d189fe

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Diff for: src/FlatfileExport.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,32 @@ public function addRows(Collection $models)
141141
}
142142

143143
/**
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
147147
*
148148
* @return void
149149
* @throws CannotInsertRecord
150150
*/
151-
public function addRowForEachRelation(Model $model, string $relation, string $alias)
151+
public function addRowForEachRelation(Model $model, $relations, string $alias)
152152
{
153-
if (!data_get($model, $relation)) {
154-
$this->addRow($model);
153+
$relations = !is_array($relations) ? [$relations] : $relations;
154+
$hasRelation = false;
155155

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+
}
157165
}
158166

159-
foreach (data_get($model, $relation) as $relationModel) {
160-
$model->$alias = $relationModel;
167+
// has no relations, insert only one row
168+
if (!$hasRelation) {
161169
$this->addRow($model);
162-
unset($model->$alias);
163170
}
164171
}
165172

0 commit comments

Comments
 (0)