You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
<?php
namespace App\Models;
use App\Traits\HasTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AppLine extends Model
{
use HasFactory, HasTranslations;
public $translatable = ['text'];
}
app\Traits\HasTranslations.php:
<?php
namespace App\Traits;
use Spatie\Translatable\HasTranslations as BaseHasTranslations;
trait HasTranslations
{
use BaseHasTranslations;
public function toArray()
{
$attributes = $this->attributesToArray(); // attributes selected by the query
// remove attributes if they are not selected
$translatables = array_filter($this->getTranslatableAttributes(), function ($key) use ($attributes) {
return array_key_exists($key, $attributes);
});
foreach ($translatables as $field) {
$attributes[$field] = $this->getTranslation($field, \App::getLocale());
}
return array_merge($attributes, $this->relationsToArray());
}
public function toArrayWithAllTranslations()
{
return array_merge($this->attributesToArray(), $this->relationsToArray());
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
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