I've created tables: addresses, contacts, events and addressables.
Here addressables (id, address_id, addressable_id, addressable_type) has many to many polymorphic relation for address with contacts and events.
I've defined following models:
Address:
class Address extends Model
{
protected $fillable = [...];
public function addressable()
{
$this->morphTo();
}
}
Contact:
class Contact extends Model
{
protected $fillable = [...];
public function addresses()
{
return $this->morphToMany(Address::class, 'addressable');
}
}
Event:
class Event extends Model
{
protected $fillable = [...];
public function addresses()
{
return $this->morphToMany(Address::class, 'addressable');
}
}
Now I wanted to fetch all addresses by contact or event or any other models defined. Is it possible to fetch addresses by specific type without defining following functions in Address model? Any help is much appreciated.
public function contacts()
{
return $this->morphedByMany('Contact', 'addressable');
}
public function events()
{
return $this->morphedByMany('Event', 'addressable');
}
If possible how could I achieve the solution?
Expected:
$contactAddress = Address::getAddressable('App\Contact');
$eventAddress = Address::getAddressable('App\Event');
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Lt5TSh
via IFTTT
Aucun commentaire:
Enregistrer un commentaire