samedi 10 août 2019

laravel get all record and append suitable translate object to it

in my app user can choose many languages... in my home page i want to show all hotels + requested language .

in my home page controller i write this :

it only return one hotel record for the requested locale key... i want to show all hotel and append the suitable translate to it if ready. any idea?

   $lang = app()->getLocale();
        $hotels = Hotel::whereHas('HotelTranslates.language', function ($q) use ($lang) {
            $q->where('name', $lang);
        })->get();

DB STRUCTURE:

hotels table:

  id(int)
  user(int)
  name
  manager
  ...

hotels_translates table:

   id (int)
   language_id(int)
   hotel_id (int)
   title
   meta
   keywords
   ...

language Table:

  id(int)
  name
  slug

in Hotel model:

 public function HotelTranslates()
    {
        return $this->hasMany(HotelTranslate::class);
    }

HotelTranslate model:

  public function hotel()
    {
        return $this->belongsTo(Hotel::class);
    }



      public function language(){
            return $this->belongsTo(Language::class);
        }

language model:

public function translates(){
        return $this->hasMany(HotelTranslate::class);
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire