Customizing the toArray method #432
Unanswered
overclocked555
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thanks for the great package!
I'm using several models with translatable fields.
When serializing a model, I usually need to load translatable fields in the current locale. This example is described in the documentation and I am using it.
\App\Models\AppLine::get()->toArray()
However, sometimes I need to load all instances of a model with all translations.
I'm trying to add a new function
toArrayWithAllTranslations()
to a trait.But I'm getting this error
\App\Models\AppLine::get()->toArrayWithAllTranslations()
BadMethodCallException Method Illuminate\Database\Eloquent\Collection::toArrayWithAllTranslations does not exist.
I guess I don't understand very well how traits work in Laravel.
I plan to also add events to all translatable models.
Where is the right place to place this function and events for use across multiple models?
app\Models\AppLine.php:
app\Traits\HasTranslations.php:
Beta Was this translation helpful? Give feedback.
All reactions