2017-10-16 2 views
2

Ich habe eine Frage mit Laravel Beziehung OneToMany. Meine Datenbank hat 2 Tabelle product und product_entry. Produkt wird Informationen haben, um den gleichen Preis zu wählen ... und product_entry hat Informationen über Infomation.Laravel - Wählen Kind, wo param

1 Produkt haben viele product_entry wird eine andere Sprache. Aber Seite nur 1 Sprache in der gleichen Zeit, so habe ich sowieso nur product_entry haben product_entry_language gleiche aktuelle Sprache?

Beispiel: Ich habe 1 Produkt a mit Sprache "en" und "es". Die normale, aktuelle Sprache ist "en". In Klinge muss i produkt-> product_entries foreach und wenn (product_entry_language == "en") {** erhalten Info **} => Ich will nicht brauchen laufen foreach

Modell

class Product extends Model 
{ 
    use TransformableTrait; 

    protected $table = 'product'; 
    protected $fillable = ['product_code','product_id']; 

    public function entries() 
    { 
     return $this->hasMany('App\Entities\ProductEntry', 'product_entry_product_id', 'product_id'); 
    } 

} 

class ProductEntry extends Model 
{ 
    use TransformableTrait; 
    protected $table = 'product_entry'; 
    protected $fillable = ['product_entry_product_id','product_entry_name']; 

} 

enter image description here

+0

bitte posten zu Ihren Modellen. –

+0

Ich hatte Model hinzugefügt –

Antwort

2

Sie können diese

Product::with(['entries' => function ($q) { 
    $q->where('product_entry_language', \App::getLocale()); 
}])->get(); 

\App::getLocale() von

tun, ist die aktuelle Laravel locale

0

Verwenden Sie ein constraint, zum Beispiel:

Product::with(['product_entry' => function ($q) use($language) { 
    $q->where('language', $language); 
}])->get(); 

Wo product_entry Beziehungsname ist.