/** * Get the pivot attribute from a model. * * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Relations\Relation $parentRelation */ public static function loadPivotAttribute(Model $model, Relation $parentRelation = null) { $attributes = $model->getAttributes(); foreach ($attributes as $key => $value) { if ($key === 'pivot') { unset($model[$key]); $pivot = $parentRelation->newExistingPivot($value); $model->setRelation($key, $pivot); } } }