Open Dezmonter opened 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.
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?
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