Skip to content

Commit 93e8c36

Browse files
authored
Allow hooking the queueable collection logic
This allows the use of parent/child Models in the same collection, to be properly serialized using the parent model (given a proper trait that facilitates the new function). Parent/child models as implemented in for example (https://github.com/calebporzio/parental) currently cause serialization errors when attempting to queue or otherwise serialize a collection containing them. This hook would allow us to alleviate the issue.
1 parent e0d8e3d commit 93e8c36

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/Illuminate/Database/Eloquent/Collection.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,17 +665,33 @@ public function getQueueableClass()
665665
return;
666666
}
667667

668-
$class = get_class($this->first());
668+
$class = $this->getModelClass($this->first());
669669

670670
$this->each(function ($model) use ($class) {
671-
if (get_class($model) !== $class) {
671+
if ($this->getModelClass($model) !== $class) {
672672
throw new LogicException('Queueing collections with multiple model types is not supported.');
673673
}
674674
});
675675

676676
return $class;
677677
}
678678

679+
/**
680+
* Get the identifiers for all of the entities.
681+
*
682+
* @return array<int, mixed>
683+
*/
684+
protected function getModelClass($model)
685+
{
686+
if(method_exists($model,'getClassNameForSerialization')) {
687+
return $model->getClassNameForSerialization();
688+
}
689+
690+
return get_class($model);
691+
}
692+
693+
694+
679695
/**
680696
* Get the identifiers for all of the entities.
681697
*

0 commit comments

Comments
 (0)