From 2680fa6dfeccd999d00d4df0af8ab99bec2f68f5 Mon Sep 17 00:00:00 2001 From: Gerardo Ibarra Date: Thu, 9 Feb 2023 16:29:50 -0300 Subject: [PATCH] check preventAccessingMissingAttributes user_id default owned --- src/Database/Models.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Database/Models.php b/src/Database/Models.php index 1455011b..e2f97d87 100644 --- a/src/Database/Models.php +++ b/src/Database/Models.php @@ -186,6 +186,7 @@ public static function ownedVia($model, $attribute = null) public static function isOwnedBy(Model $authority, Model $model) { $type = get_class($model); + $isDefaultAttribute = false; if (isset(static::$ownership[$type])) { $attribute = static::$ownership[$type]; @@ -193,9 +194,10 @@ public static function isOwnedBy(Model $authority, Model $model) $attribute = static::$ownership['*']; } else { $attribute = strtolower(static::basename($authority)).'_id'; + $isDefaultAttribute = true; } - return static::isOwnedVia($attribute, $authority, $model); + return static::isOwnedVia($attribute, $authority, $model, $isDefaultAttribute); } /** @@ -204,14 +206,19 @@ public static function isOwnedBy(Model $authority, Model $model) * @param string|\Closure $attribute * @param \Illuminate\Database\Eloquent\Model $authority * @param \Illuminate\Database\Eloquent\Model $model + * @param bool $isDefaultAttribute * @return bool */ - protected static function isOwnedVia($attribute, Model $authority, Model $model) + protected static function isOwnedVia($attribute, Model $authority, Model $model, $isDefaultAttribute = false) { if ($attribute instanceof Closure) { return $attribute($model, $authority); } + if ($isDefaultAttribute && !array_key_exists($attribute, $model->getAttributes())) { + return false; + } + return $authority->getKey() == $model->{$attribute}; }