john123's avatar

Making 2 Translation relationship definitions in Model

Hello Everyone I hope you are doing great

For context I am building project and using Filament 4 dashboard and later on I will build api for front end use. The translation method I decided to use is to make Model and ModelTranslation and make ModelTranslation store the translatable fields in different database table

so inside the Model Country I want to define the translation relationship

public function translations(): HasMany
{
    $contentLocale = session('content_locale', config('app.locale'));
    return $this->hasMany(CountryTranslation::class)
        ->where('language_id', function ($query) use ($contentLocale) {
            $query->select('id')
                ->from('languages')
                ->where('locale', $contentLocale)
                ->limit(1);
        });
}

so defining the relationship like this gives me many benefits

in any Filament resource during CRUD I dont need to specifically define which language to retrieve or alter since the relationship reads from the local inside my filament application.

here comes the problem :

Doing this is good in Context of the Dashboard , but when it comes to the api this is inaccurate since the language will be defined by the parameters sent in the request.

so what I came up with is to make 2 relationships of the translation

dashboardTranslations()

and

apiTranslations()

the Pros is that now the dashboard translations are going to be handled pretty easily while not worried about the api

but honestly doing 2 relationships like this makes me feel like I am writing awfully dirty code especially since I will make these 2 relationships in all translatable models .

the alternative is to make 1 generic relationship

return $this->hasMany(CountryTranslation::class);

and in each Filament resource and api define how to insert into the database

so my question is , what do you guys think?

any other alternative is welcome

Thank you

0 likes
0 replies

Please or to participate in this conversation.