Laravel Version
13.2.0
PHP Version
8.5.2
Database Driver & Version
No response
Description
When using the following trait:
trait SomeTrait
{
protected function initializeSomeTrait()
{
$this->mergeAppends(['url']);
}
}
on the following model:
#[Appends(['name'])]
class MyModel extends Model
{
use SomeTrait;
}
The final appendable attributes are set to ['url'], even though mergeAppends() was used in the trait's initialize method. The same applies for calling mergeHidden(), mergeVisible(), mergeFillable() and mergeGuarded() in a trait initializer.
This used to work fine in Laravel 12 and below, but breaks with the current parsing implementation of Attributes. Could this be an unanticipated breaking change?
|
if (empty($this->appends)) { |
|
$this->appends = static::resolveClassAttribute(Appends::class, 'columns') ?? []; |
|
} |
I've tried to remove the conditional and apply mergeAppends() here as well. That works. But when doing the same trick for fillable and guarded properties things are becoming complicated (there's a fillable precedence rule, and guarded has additional * logic). I've even considered to have all framework traits initialize before anything else, but again the guarded logic is pesky.
So I'm raising it here instead.
Steps To Reproduce
#[Appends(['name'])
class TestModel extends Model
{
use TestTrait;
}
trait TestTrait
{
protected function initializeTestTrait()
{
$this->mergeAppends(['email']);
}
}
public function test_it()
{
$model = new TestModel;
$this->assertEqualsCanonicalizing(['name', 'email'], $model->getAppends()); // Currently fails.
}
Laravel Version
13.2.0
PHP Version
8.5.2
Database Driver & Version
No response
Description
When using the following trait:
on the following model:
The final appendable attributes are set to
['url'], even thoughmergeAppends()was used in the trait's initialize method. The same applies for callingmergeHidden(),mergeVisible(),mergeFillable()andmergeGuarded()in a trait initializer.This used to work fine in Laravel 12 and below, but breaks with the current parsing implementation of Attributes. Could this be an unanticipated breaking change?
framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Lines 217 to 219 in 7106836
I've tried to remove the conditional and apply
mergeAppends()here as well. That works. But when doing the same trick for fillable and guarded properties things are becoming complicated (there's a fillable precedence rule, and guarded has additional*logic). I've even considered to have all framework traits initialize before anything else, but again the guarded logic is pesky.So I'm raising it here instead.
Steps To Reproduce