/** * Return a collection of the child object, given the parent object. * @param Model $model * @param string|Model $child_object * @return Collection */ public static function collection(Model $model, $child_object) { if ($child_object instanceof Model) { $child_object = get_class($child_object); } $relationships = Relationship::children(get_class($model), $model->id, $child_object); if ($relationships->isEmpty()) { return with(new $child_object())->newCollection(); } $ids = $relationships->pluck('child_id')->toArray(); return $child_object::whereIn('id', $ids)->orderBy(DB::raw('FIELD(`id`, ' . join(",", $ids) . ')'))->get(); }