mercredi 28 août 2019

get correct record form many to many relation accoring to locale lang

i have 3 table Hotel, Hotel_feature, Hotel_Hotel_feature

hotel and hotel_feature have many to many relation and Hotel_Hotel_feature is pivot table. also i have another table named languages that i save language name and slug.

tables structure:

HOTEL: 
    id,
   name,
  ..

HOTEL_FEATURE:
  id
  language_id
  name
  slug

 HOTEL_HOTEL_FEATUE
   hotel_id
   hotel_feature_id

 LANGUAGE 
   id
   name
   slug

i wrote a accessor in Hotel model to get current hotel features according to current locale lang

public function getFeatureTranslateAttribute()
{
    $lang = app()->getLocale();

    // get correct features for current hotel model
    $features = $this->hotelFeatures()->whereHas('language', function($q) use ($lang){
        $q->where('name', 'en');
    })->where('hotel_id', $this->id)->first();


    dd($features);

    return $features ?: $this;

}

but it returning null ... how can i get correct hotel features for current hotel according to locale?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire