php - Laravel 5.2 Eloquent - how to automatically fetch translated content using separate translations table -
i wondering how can make eloquent automatically fetch translated content database me, without me having specify it. example, instead of having run following:
$article = article::sometranslationmethod("en")->first(); //to english version $article = article::sometranslationmethod("de")->first(); //to german version $article = article::sometranslationmethod()->first(); //to current locale version //etc
i able run:
$article = article::first();
and have result translated - according current locale or whatever.
details:
my database schema consists of articles table, , translations table. (please excuse german):
articles id | title | body ------------------------------------------------------------------------ 1 | how drink coffee | hi, article coffee. translations id | language | remote_table | remote_id | remote_field | value -------------------------------------------------------------------------------------------------------- 1 | en | articles | 1 | title | how drink coffee 2 | de | articles | 1 | title | wie sollte man seinen kaffee trinken 3 | en | articles | 1 | body | hi, article coffee. 4 | de | articles | 1 | body | hallo, dieser text geht um kaffee.
so when locale set "en": $article->title
should have value "how drink coffee"
, when locale set "de": $article->title
should have value "wie sollte man seinen kaffee trinken"
.
using activerecord in past, able achieve running function right after result fetched database. translations
table translations, , replace $article
object's properties translations. , it. every time had fetch article, run $article = myactiverecord::findfirst('articles');
, , confident result holds translated values, without me having take further action achieve - after.
but using laravel eloquent, not possible anymore. @ least, not out of box. (and if was, mean changing eloquent's source code - not option). therefore, has manually implemented. right can think of 3 possible approaches:
one possible approach have custom base model extends eloquent\model class, have models extend custom one, , in custom model somehow add functionality. problem is, not know how (and if) can add functionality.
another way use eloquent relationship.
$article->hasmany("translations")
. problem in case that, in order specify such relationship, 1 foreign key not enough,translations
table holds translations every other table in database. article represented intranslations
table not id, combination ofremote_table
,remote_id
,remote_field
. , if somehow create relationship, still have take further action - translated content indeed included in result,$article->title
still have english value example.a third approach found while searching use accessors , mutators. although not experienced them, not see how can solve problem, not question of data formatting, rather of replacing data table. plus, not rid me of having take further action after fetching data.
my bets go extending eloquent\model class, , somehow making happen in there. not see how.
any thoughts appreciated.
i think can achieve global scope applied article
queries.
open article
model , locate public static function boot()
(or create if not exist)
public static function boot() { parent::boot(); static::addglobalscope('transliterated', function(builder $builder){ return $builder->translate(app()->getlocale()); }); }
this should work out of box , apply queries made against article::
Comments
Post a Comment