Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Phaseolies/Database/Entity/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,14 +1017,15 @@ public function toArray(): array
$attributes = $this->makeVisible();

foreach ($this->relations as $key => $relation) {
if ($relation instanceof Model) {
if ($relation === null) {
$attributes[$key] = null;
} elseif ($relation instanceof Model) {
$attributes[$key] = $relation->toArray();
} elseif ($relation instanceof Collection) {
$attributes[$key] = $relation->map(function ($item) {
$result = $item instanceof Model ? $item->toArray() : (array)$item;
if (isset($item->pivot_data)) {
$result['pivot'] = (array)$item->pivot_data;
unset($result['pivot_data']);
if (isset($item->pivot)) {
$result['pivot'] = (array)$item->pivot;
}
return $result;
})->all();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,6 @@ public static function count(): int
return static::query()->count();
}

/**
* Converts the model's attributes to an array.
*
* @return array.
*/
public function toArray(): array
{
$attributes = $this->makeVisible();

// Include loaded relationships
foreach ($this->relations as $key => $relation) {
if ($relation === null) {
$attributes[$key] = null;
} elseif ($relation instanceof Model) {
$attributes[$key] = $relation->toArray();
} elseif ($relation instanceof Collection) {
$attributes[$key] = $relation->all();
} else {
$attributes[$key] = $relation;
}
}

return $attributes;
}

/**
* Pluck an array of values from a single column.
*
Expand Down Expand Up @@ -265,7 +240,7 @@ public function getDirtyAttributes(): array
foreach ($this->attributes as $key => $value) {
if (
!array_key_exists($key, $this->originalAttributes) ||
$this->originalAttributes[$key] != $value
!$this->valuesAreEqual($this->originalAttributes[$key], $value)
) {
$dirty[$key] = $value;
}
Expand All @@ -274,6 +249,21 @@ public function getDirtyAttributes(): array
return $dirty;
}

/**
* Compare original and current values for equality, treating nulls as equal
*
* @param mixed $original
* @param mixed $current
* @return bool
*/
protected function valuesAreEqual(mixed $original, mixed $current): bool
{
if ($original === null && $current === null) return true;
if ($original === null || $current === null) return false;

return (string)$original === (string)$current;
}

/**
* Insert multiple records into the database
*
Expand Down
Loading