You're close, but the syntax is wrong. Should be:
public function setCompanyNameAttribute($value)
{
$this->attributes['company_name'] = strtoupper($value);
}
https://laravel.com/docs/5.6/eloquent-mutators#defining-a-mutator
I have a client model with a mutator:
public function setCompanyNameAttribute($value)
{
return strtoupper($value);
}
And a factory
$factory->define(Client::class, function (Faker $faker) {
return [
'company_name' => $faker->company,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'primary_phone` => $faker->phoneNumber,
];
});
When the factory is called, I do not receive the company_name as an attribute:
$client = factory(Client::class)->make();
dd($client);
-----
#attributes: array:3 [
"first_name" => "Anna"
"last_name" => "Mills"
"primary_phone" => "334-717-4998"
]
When I remove the mutator, all 4 attributes are present:
#attributes: array:4 [
"company_name" => "Weissnat, Hilpert and Osinski"
"first_name" => "Jaylon"
"last_name" => "Quitzon"
"primary_phone" => "602-948-8484"
]
I added protected $fillable = ['company_name'] and have no $guarded but the company_name still doesn't show in the attributes list.
Is this a known issue, or do I need to add something else to my client model? Most of my tests broke with the addition of the mutator.
(edit: markdown...)
You're close, but the syntax is wrong. Should be:
public function setCompanyNameAttribute($value)
{
$this->attributes['company_name'] = strtoupper($value);
}
https://laravel.com/docs/5.6/eloquent-mutators#defining-a-mutator
Please or to participate in this conversation.