Skip to content

Commit 858df49

Browse files
author
Pantea Marius-ciclistu
committed

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

src/Eloquent/CustomRelations/HasCleverRelationships.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,27 @@ public function __construct(
397397
$relationName
398398
);
399399
}
400+
401+
/**
402+
* @inheritdoc
403+
*/
404+
protected function migratePivotAttributes(Model $model): array
405+
{
406+
$values = [];
407+
408+
foreach (\array_keys($model->getAttributes(true)) as $key) {
409+
// To get the pivots attributes we will just take any of the attributes which
410+
// begin with "pivot_" and add those to this arrays, as well as unsetting
411+
// them from the parent's models since they exist in a different table.
412+
if (str_starts_with($key, 'pivot_')) {
413+
$values[substr($key, 6)] = $model->getAttributeFromArray($key);
414+
415+
unset($model->$key);
416+
}
417+
}
418+
419+
return $values;
420+
}
400421
};
401422
}
402423

src/Models/BaseModel.php

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MacropaySolutions\LaravelCrudWizard\Models;
44

55
use Carbon\Carbon;
6+
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Support\Collection;
89
use Illuminate\Support\Facades\Cache;
@@ -478,7 +479,13 @@ protected function initializeActiveRecordSegregationProperties(): void
478479
*/
479480
public function __call($method, $parameters)
480481
{
481-
if (\in_array(\strtolower($method), ['incrementeach', 'decrementeach'], true)) {
482+
$lowerMethod = \strtolower($method);
483+
484+
if ($lowerMethod === 'getattributefromarray') {
485+
return $this->$method(...$parameters);
486+
}
487+
488+
if (\in_array($lowerMethod, ['incrementeach', 'decrementeach'], true)) {
482489
/** \MacropaySolutions\MaravelRestWizard\Models\BaseModel::incrementBulk can be used instead */
483490
throw new \BadMethodCallException(\sprintf(
484491
'Call to unscoped method %s::%s(). Use $model->newQuery()->getQuery()->%s()' .
@@ -555,4 +562,106 @@ public function unique($key = null, $strict = false): Collection
555562
}
556563
};
557564
}
565+
566+
/**
567+
* Get all of the current attributes on the model.
568+
* @param bool $withoutCasting
569+
* @return array
570+
*/
571+
public function getAttributes(): array
572+
{
573+
if (true !== \func_get_arg(0)) {
574+
$this->mergeAttributesFromCachedCasts();
575+
}
576+
577+
return $this->attributes;
578+
}
579+
580+
/**
581+
* @inheritdoc
582+
*/
583+
public function syncOriginalAttributes($attributes): static
584+
{
585+
$attributes = is_array($attributes) ? $attributes : func_get_args();
586+
587+
foreach ($attributes as $attribute) {
588+
$this->original[$attribute] = $this->getAttributeFromArray($attribute);
589+
}
590+
591+
return $this;
592+
}
593+
594+
/**
595+
* Get an attribute from the $attributes array without transformation
596+
* @see self::getAttributeValue
597+
*
598+
* @param string $key
599+
* @return mixed
600+
*/
601+
protected function getAttributeFromArray($key): mixed
602+
{
603+
$this->mergeAttributesFromClassCasts($key);
604+
$this->mergeAttributesFromAttributeCasts($key);
605+
606+
return $this->attributes[$key] ?? null;
607+
}
608+
609+
/**
610+
* Merge the cast class attributes back into the model.
611+
* @param string|array $keys
612+
* @return void
613+
*/
614+
protected function mergeAttributesFromClassCasts(): void
615+
{
616+
$k = \func_get_args()[0] ?? null;
617+
618+
$classCastCache = \is_string($k) || \is_array($k) ?
619+
\array_intersect_key($this->classCastCache, \array_flip(\array_values((array)$k))) :
620+
$this->classCastCache;
621+
622+
foreach ($classCastCache as $key => $value) {
623+
$caster = $this->resolveCasterClass($key);
624+
625+
$this->attributes = array_merge(
626+
$this->attributes,
627+
$caster instanceof CastsInboundAttributes
628+
? [$key => $value]
629+
: $this->normalizeCastClassResponse($key, $caster->set($this, $key, $value, $this->attributes))
630+
);
631+
}
632+
}
633+
634+
/**
635+
* Merge the cast class attributes back into the model.
636+
* @param string|array $keys
637+
* @return void
638+
*/
639+
protected function mergeAttributesFromAttributeCasts(): void
640+
{
641+
$k = \func_get_args()[0] ?? null;
642+
643+
$attributeCastCache = \is_string($k) || \is_array($k) ?
644+
\array_intersect_key($this->attributeCastCache, \array_flip(\array_values((array)$k))) :
645+
$this->attributeCastCache;
646+
647+
foreach ($attributeCastCache as $key => $value) {
648+
$attribute = $this->{Str::camel($key)}();
649+
650+
if ($attribute->get && !$attribute->set) {
651+
continue;
652+
}
653+
654+
$callback = $attribute->set ?: function ($value) use ($key) {
655+
$this->attributes[$key] = $value;
656+
};
657+
658+
$this->attributes = array_merge(
659+
$this->attributes,
660+
$this->normalizeCastClassResponse(
661+
$key,
662+
$callback($value, $this->attributes)
663+
)
664+
);
665+
}
666+
}
558667
}

0 commit comments

Comments
 (0)