Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.21k stars 151 forks source link

method that includes model attributes with translatable attributes #416

Open Dezmonter opened 1 week ago

Dezmonter commented 1 week ago

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 commented 1 week ago

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 commented 6 days ago

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 commented 6 days ago

https://github.com/Astrotomic/laravel-translatable/blob/250f9b3731326c3c41d1a53b000563140bcdd7f8/src/Translatable/Translatable.php#L140-L162