1313
1414namespace ApiPlatform \Laravel \Eloquent \PropertyAccess ;
1515
16- use Illuminate \Database \Eloquent \Relations \ HasMany ;
16+ use Illuminate \Database \Eloquent \Model ;
1717use Symfony \Component \PropertyAccess \PropertyAccess ;
1818use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
1919use 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