sql >> Database >  >> RDS >> Mysql

Hoe Relation::morphMap() te gebruiken voor verschillende klassen

Laten we beginnen met het definiëren van de polymorfe relaties

Eerste relatie....

Package.php

public function provider() {
  return $this->morphTo(null, 'map_type_id', 'map_id');
}

Venue.php

public function packages() {
  // you should provide the relation name, in our exemple its called `provider` as a second parameter
  return $this->morphMany(VendorPackage::class, 'provider', 'venues');
}

Vendor.php

public function packages() {
  // you should provide the relation name, in our exemple its called `provider` as a second parameter
  return $this->morphMany(VendorPackage::class, 'provider', 'vendors');
}

Tweede relatie

TicketBooking.php

public function provider() {
  return $this->morphTo(null, 'map_type_id', 'map_id');
}

Venue.php

public function bookings() { 
  return $this->morphMany(TicketBooking::class, 'provider', 'venues');
}

Decors.php

public function bookings() {
  return $this->morphMany(TicketBooking::class, 'provider', 'decors');
}

en registreer Relation::morphMap als

Relation::morphMap([
  'vendors' => \Vendor::class,
  'venues' =>  \Venue::class,
  'decors' =>  \Decor::class
]);



  1. DBMS_JOB versus DBMS_SCHEDULER

  2. Wijs resultaat van dynamische sql toe aan variabele

  3. mysql telt geen dubbele rijen

  4. Polymorf-achtige SQL-databasetabelstructuur