Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method that includes model attributes with translatable attributes #416

Open
Dezmonter opened this issue Jun 27, 2024 · 3 comments
Open
Labels

Comments

@Dezmonter
Copy link

Dezmonter commented Jun 27, 2024

Please tell me a method that includes model attributes with translatable attributes in the package

For example, we write instead of $post->translations->name - $post->name

@Gummibeer
Copy link
Member

Hey,

sorry but I have no idea what you mean. In case you are after a toArray() like method for APIs: Eloquent API resources will be my one and only all time answer.

https://laravel.com/docs/11.x/eloquent-resources

I'm in no way interested in adding anything that would promote toArray() usage in any way.

@Dezmonter
Copy link
Author

Dezmonter commented Jun 28, 2024

I made a new scope function

public function scopeWithTranslationOnly(Builder $query, string $translationField)
{
  $query->with([
    'translations' => function (Relation $query) use ($translationField) {
        return $query->select($translationField, $this->getTranslationRelationKey())
            ->where($this->getTranslationsTable().'.'.'language_id', LanguageFacade::getLanguage()->id);
    },
  ]);
}

But if you apply $doctorSpecialities = DoctorSpeciality::withTranslationOnly('label')->get();

I get

{
   "id":1,
   "image":"docspec.svg",
   "sort_order":23,
   "created_at":"2024-06-26T21:19:14.000000Z",
   "updated_at":"2024-06-26T21:19:14.000000Z",
   "label":null,
   "name":null,
   "name_plural":null,
   "slug":null,
   "meta_title":null,
   "meta_description":null,
   "translations":[
      {
         "label":"Dudley Schmitt",
         "doctor_speciality_id":1
      }
   ]
}

data is not filled in label - how to make them fill up ?

How are model attributes combined with the attributes of the translated model?

@Gummibeer
Copy link
Member

public function getAttribute($key)
{
[$attribute, $locale] = $this->getAttributeAndLocale($key);
if ($this->isTranslationAttribute($attribute)) {
if ($this->getTranslation($locale) === null) {
return $this->getAttributeValue($attribute);
}
// If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue
// on it. This way, we can use Eloquent's checking for Mutation, type casting, and
// Date fields.
if ($this->hasGetMutator($attribute)) {
$this->attributes[$attribute] = $this->getAttributeOrFallback($locale, $attribute);
return $this->getAttributeValue($attribute);
}
return $this->getAttributeOrFallback($locale, $attribute);
}
return parent::getAttribute($key);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants