Skip to content

Commit 05e550d

Browse files
Fix Table attribute incrementing not working for Pivot models (#59336)
* Fix Table attribute incrementing not working for Pivot models The `#[Table]` attribute includes an argument to specify whether the primary key should be incrementing. However, for Pivot models that default to non-incrementing, the Table attribute's incrementing argument was not being applied because the logic only checked it when `$this->incrementing === true`. This fix removes the outer condition so that the Table attribute can override the default incrementing behavior for all models, including Pivots. * Update Model.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 8aa511e commit 05e550d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,10 @@ public function initializeModelAttributes()
446446
$this->keyType = $table->keyType;
447447
}
448448

449-
if ($this->incrementing === true) {
450-
if (static::resolveClassAttribute(WithoutIncrementing::class) !== null) {
451-
$this->incrementing = false;
452-
} elseif ($table && $table->incrementing !== null) {
453-
$this->incrementing = $table->incrementing;
454-
}
449+
if (static::resolveClassAttribute(WithoutIncrementing::class) !== null) {
450+
$this->incrementing = false;
451+
} elseif ($table && $table->incrementing !== null) {
452+
$this->incrementing = $table->incrementing;
455453
}
456454
}
457455

tests/Database/DatabaseEloquentModelAttributesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ public function test_dedicated_without_incrementing_attribute_overrides_table_in
107107
$this->assertFalse($model->getIncrementing());
108108
}
109109

110+
public function test_table_attribute_incrementing_applies_to_pivot_models(): void
111+
{
112+
$model = new PivotWithIncrementing;
113+
114+
$this->assertTrue($model->getIncrementing());
115+
}
116+
110117
public function test_connection_attribute(): void
111118
{
112119
$model = new ModelWithConnectionAttribute;
@@ -504,3 +511,9 @@ class ModelWithWithoutIncrementingAttributeOverride extends Model
504511
{
505512
//
506513
}
514+
515+
#[Table(incrementing: true)]
516+
class PivotWithIncrementing extends \Illuminate\Database\Eloquent\Relations\Pivot
517+
{
518+
//
519+
}

0 commit comments

Comments
 (0)