vendredi 31 août 2018

Querying Many to Many polymorphic relationship in laravel

I've following tables:

  • contacts(id, name, email, phone_no),
  • events(id, title, start_date, end_date),
  • addresses(id, city, state, country, zip_code),
  • addressables(id, address_id, addressable_id, addressable_type)

Here addressables have many to many polymorphic relation for contacts events. addressables could also hold other polymorphic model class.

Addres Model

public function addressable()
{
    return $this->morphTo();
}

Event/Contact Model

public function addresses()
{
    return $this->morphToMany(Address::class, 'addressable');
}

I wanted to fetch addresses along with their associated models. I would appreciate if anyone could help me to

  • List addresses with associated models
  • List addresses for specific model type
  • List addresses group by model type

Thank you in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire