Skip to content

Commit da37c4b

Browse files
Merge pull request #8 from danieleisenhardt/eloquent-fix
Compatibility with Laravel's [8.x] Attribute Cast / Accessor Improvements
2 parents 5692e8c + cd28a19 commit da37c4b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/MenuItem.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,31 @@ public function toArray()
222222
return array_merge($this->attributesToArray(), $this->variablesToArray());
223223
}
224224

225+
/**
226+
* Convert the model's attributes to an array.
227+
*
228+
* @return array
229+
*/
230+
public function attributesToArray()
231+
{
232+
// If an attribute is a date, we will cast it to a string after converting it
233+
// to a DateTime / Carbon instance. This is so we will get some consistent
234+
// formatting while accessing attributes vs. arraying / JSONing a model.
235+
$attributes = $this->addDateAttributesToArray(
236+
$attributes = $this->getArrayableAttributes()
237+
);
238+
239+
240+
// Here we will grab all of the appended, calculated attributes to this model
241+
// as these attributes are not really in the attributes array, but are run
242+
// when we need to array or JSON the model for convenience to the coder.
243+
foreach ($this->getArrayableAppends() as $key) {
244+
$attributes[$key] = $this->mutateAttributeForArray($key, null);
245+
}
246+
247+
return $attributes;
248+
}
249+
225250
/**
226251
* Convert the object to its JSON representation.
227252
*

0 commit comments

Comments
 (0)