Skip to content

Commit fcd458f

Browse files
committed
fix: Eloquent PropertyAccessor
1 parent b8368c6 commit fcd458f

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/Laravel/Eloquent/PropertyAccess/PropertyAccessor.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Laravel\Eloquent\PropertyAccess;
1515

16-
use Illuminate\Database\Eloquent\Relations\HasMany;
16+
use Illuminate\Database\Eloquent\Model;
1717
use Symfony\Component\PropertyAccess\PropertyAccess;
1818
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1919
use Symfony\Component\PropertyAccess\PropertyPathInterface;
@@ -36,6 +36,12 @@ public function __construct(
3636
*/
3737
public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value): void
3838
{
39+
if ($objectOrArray instanceof Model) {
40+
$objectOrArray->{$propertyPath} = $value;
41+
42+
return;
43+
}
44+
3945
$this->inner->setValue($objectOrArray, $propertyPath, $value);
4046
}
4147

@@ -44,20 +50,22 @@ public function setValue(object|array &$objectOrArray, string|PropertyPathInterf
4450
*/
4551
public function getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): mixed
4652
{
47-
$value = $this->inner->getValue($objectOrArray, $propertyPath);
48-
49-
if ($value instanceof HasMany) {
53+
if ($objectOrArray instanceof Model) {
5054
return $objectOrArray->{$propertyPath};
5155
}
5256

53-
return $value;
57+
return $this->inner->getValue($objectOrArray, $propertyPath);
5458
}
5559

5660
/**
5761
* @param array<mixed, mixed>|object $objectOrArray
5862
*/
5963
public function isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool
6064
{
65+
if ($objectOrArray instanceof Model) {
66+
return true;
67+
}
68+
6169
return $this->inner->isWritable($objectOrArray, $propertyPath);
6270
}
6371

@@ -66,6 +74,10 @@ public function isWritable(object|array $objectOrArray, string|PropertyPathInter
6674
*/
6775
public function isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool
6876
{
77+
if ($objectOrArray instanceof Model) {
78+
return true;
79+
}
80+
6981
return $this->inner->isReadable($objectOrArray, $propertyPath);
7082
}
7183
}

0 commit comments

Comments
 (0)