From 44e6a294e4441e9e3338008af0288979b3f677e8 Mon Sep 17 00:00:00 2001 From: taylorotwell <463230+taylorotwell@users.noreply.github.com> Date: Wed, 23 Apr 2025 14:50:58 +0000 Subject: [PATCH 1/4] Update CHANGELOG --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0caa4b2b4d2e..0c196a99d749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Release Notes for 12.x -## [Unreleased](https://github.com/laravel/framework/compare/v12.10.0...12.x) +## [Unreleased](https://github.com/laravel/framework/compare/v12.10.1...12.x) + +## [v12.10.1](https://github.com/laravel/framework/compare/v12.10.0...v12.10.1) - 2025-04-23 + +* Revert "Use value() helper in 'when' method to simplify code" #55465 by [@mohammadrasoulasghari](https://github.com/mohammadrasoulasghari) in https://github.com/laravel/framework/pull/55514 +* [12.x] Use xxh128 when comparing views for changes by [@shawnlindstrom](https://github.com/shawnlindstrom) in https://github.com/laravel/framework/pull/55517 +* [12.x] Ensure related models is iterable on `HasRelationships@relationLoaded()` by [@rodrigopedra](https://github.com/rodrigopedra) in https://github.com/laravel/framework/pull/55519 +* [12.x] Add Enum support for assertJsonPath in AssertableJsonString.php by [@azim-kordpour](https://github.com/azim-kordpour) in https://github.com/laravel/framework/pull/55516 ## [v12.10.0](https://github.com/laravel/framework/compare/v12.9.2...v12.10.0) - 2025-04-22 From f5fba9cb1aea3ae9963d9958e1ce6d2e9715d316 Mon Sep 17 00:00:00 2001 From: Rodrigo Pedra Brum Date: Thu, 24 Apr 2025 11:05:39 -0300 Subject: [PATCH 2/4] [12.x] Address Model@relationLoaded when relation is null (#55531) * address when relation is null * added tests and return false when $relatedModels is empty --- .../Eloquent/Concerns/HasRelationships.php | 6 +++++- .../Database/EloquentModelRelationLoadedTest.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index fc211e179a53..b27b71109096 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -1088,7 +1088,11 @@ public function relationLoaded($key) if ($nestedRelation !== null) { $relatedModels = is_iterable($relatedModels = $this->$relation) ? $relatedModels - : [$relatedModels]; + : array_filter([$relatedModels]); + + if (count($relatedModels) === 0) { + return false; + } foreach ($relatedModels as $related) { if (! $related->relationLoaded($nestedRelation)) { diff --git a/tests/Integration/Database/EloquentModelRelationLoadedTest.php b/tests/Integration/Database/EloquentModelRelationLoadedTest.php index 548a50d11957..265926d9cd4c 100644 --- a/tests/Integration/Database/EloquentModelRelationLoadedTest.php +++ b/tests/Integration/Database/EloquentModelRelationLoadedTest.php @@ -135,6 +135,21 @@ public function testWhenParentRelationIsASingleInstance() $this->assertTrue($model->relationLoaded('two.one')); $this->assertTrue($model->two->one->is($one)); } + + public function testWhenRelationIsNull() + { + $one = One::query()->create(); + $two = $one->twos()->create(); + $three = $two->threes()->create(); + + $model = Three::query() + ->with('one.twos') + ->find($three->id); + + $this->assertTrue($model->relationLoaded('one')); + $this->assertNull($model->one); + $this->assertFalse($model->relationLoaded('one.twos')); + } } class One extends Model From dc5b445c0a9aa794f3b6d04b3bf10ed5b00c7814 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 24 Apr 2025 09:07:27 -0500 Subject: [PATCH 3/4] revert complicated changes --- .../Eloquent/Concerns/HasRelationships.php | 31 +-- .../EloquentModelRelationLoadedTest.php | 199 ------------------ 2 files changed, 1 insertion(+), 229 deletions(-) delete mode 100644 tests/Integration/Database/EloquentModelRelationLoadedTest.php diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index b27b71109096..79a2f5d98cd0 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -1072,36 +1072,7 @@ public function getRelation($relation) */ public function relationLoaded($key) { - if (array_key_exists($key, $this->relations)) { - return true; - } - - [$relation, $nestedRelation] = array_replace( - [null, null], - explode('.', $key, 2), - ); - - if (! array_key_exists($relation, $this->relations)) { - return false; - } - - if ($nestedRelation !== null) { - $relatedModels = is_iterable($relatedModels = $this->$relation) - ? $relatedModels - : array_filter([$relatedModels]); - - if (count($relatedModels) === 0) { - return false; - } - - foreach ($relatedModels as $related) { - if (! $related->relationLoaded($nestedRelation)) { - return false; - } - } - } - - return true; + return array_key_exists($key, $this->relations); } /** diff --git a/tests/Integration/Database/EloquentModelRelationLoadedTest.php b/tests/Integration/Database/EloquentModelRelationLoadedTest.php deleted file mode 100644 index 265926d9cd4c..000000000000 --- a/tests/Integration/Database/EloquentModelRelationLoadedTest.php +++ /dev/null @@ -1,199 +0,0 @@ -increments('id'); - }); - - Schema::create('twos', function (Blueprint $table) { - $table->increments('id'); - $table->integer('one_id'); - }); - - Schema::create('threes', function (Blueprint $table) { - $table->increments('id'); - $table->integer('two_id'); - $table->integer('one_id')->nullable(); - }); - } - - public function testWhenRelationIsInvalid() - { - $one = One::query()->create(); - $one->twos()->create(); - - $model = One::query() - ->with('twos') - ->find($one->id); - - $this->assertFalse($model->relationLoaded('.')); - $this->assertFalse($model->relationLoaded('invalid')); - } - - public function testWhenNestedRelationIsInvalid() - { - $one = One::query()->create(); - $one->twos()->create(); - - $model = One::query() - ->with('twos') - ->find($one->id); - - $this->assertFalse($model->relationLoaded('twos.')); - $this->assertFalse($model->relationLoaded('twos.invalid')); - } - - public function testWhenRelationNotLoaded() - { - $one = One::query()->create(); - $one->twos()->create(); - - $model = One::query()->find($one->id); - - $this->assertFalse($model->relationLoaded('twos')); - } - - public function testWhenRelationLoaded() - { - $one = One::query()->create(); - $one->twos()->create(); - - $model = One::query() - ->with('twos') - ->find($one->id); - - $this->assertTrue($model->relationLoaded('twos')); - } - - public function testWhenChildRelationIsNotLoaded() - { - $one = One::query()->create(); - $two = $one->twos()->create(); - $two->threes()->create(); - - $model = One::query() - ->with('twos') - ->find($one->id); - - $this->assertTrue($model->relationLoaded('twos')); - $this->assertFalse($model->relationLoaded('twos.threes')); - } - - public function testWhenChildRelationIsLoaded() - { - $one = One::query()->create(); - $two = $one->twos()->create(); - $two->threes()->create(); - - $model = One::query() - ->with('twos.threes') - ->find($one->id); - - $this->assertTrue($model->relationLoaded('twos')); - $this->assertTrue($model->relationLoaded('twos.threes')); - } - - public function testWhenChildRecursiveRelationIsLoaded() - { - $one = One::query()->create(); - $two = $one->twos()->create(); - $two->threes()->create(['one_id' => $one->id]); - - $model = One::query() - ->with('twos.threes.one') - ->find($one->id); - - $this->assertTrue($model->relationLoaded('twos')); - $this->assertTrue($model->relationLoaded('twos.threes')); - $this->assertTrue($model->relationLoaded('twos.threes.one')); - } - - public function testWhenParentRelationIsASingleInstance() - { - $one = One::query()->create(); - $two = $one->twos()->create(); - $three = $two->threes()->create(); - - $model = Three::query() - ->with('two.one') - ->find($three->id); - - $this->assertTrue($model->relationLoaded('two')); - $this->assertTrue($model->two->is($two)); - $this->assertTrue($model->relationLoaded('two.one')); - $this->assertTrue($model->two->one->is($one)); - } - - public function testWhenRelationIsNull() - { - $one = One::query()->create(); - $two = $one->twos()->create(); - $three = $two->threes()->create(); - - $model = Three::query() - ->with('one.twos') - ->find($three->id); - - $this->assertTrue($model->relationLoaded('one')); - $this->assertNull($model->one); - $this->assertFalse($model->relationLoaded('one.twos')); - } -} - -class One extends Model -{ - public $table = 'ones'; - public $timestamps = false; - protected $guarded = []; - - public function twos(): HasMany - { - return $this->hasMany(Two::class, 'one_id'); - } -} - -class Two extends Model -{ - public $table = 'twos'; - public $timestamps = false; - protected $guarded = []; - - public function one(): BelongsTo - { - return $this->belongsTo(One::class, 'one_id'); - } - - public function threes(): HasMany - { - return $this->hasMany(Three::class, 'two_id'); - } -} - -class Three extends Model -{ - public $table = 'threes'; - public $timestamps = false; - protected $guarded = []; - - public function one(): BelongsTo - { - return $this->belongsTo(One::class, 'one_id'); - } - - public function two(): BelongsTo - { - return $this->belongsTo(Two::class, 'two_id'); - } -} From 0f123cc857bc177abe4d417448d4f7164f71802a Mon Sep 17 00:00:00 2001 From: taylorotwell <463230+taylorotwell@users.noreply.github.com> Date: Thu, 24 Apr 2025 14:11:20 +0000 Subject: [PATCH 4/4] Update version to v12.10.2 --- src/Illuminate/Foundation/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index fb6f7548a34c..3c491b0544b2 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -45,7 +45,7 @@ class Application extends Container implements ApplicationContract, CachesConfig * * @var string */ - const VERSION = '12.10.1'; + const VERSION = '12.10.2'; /** * The base path for the Laravel installation.