2

In my project there is a Many-to-Many polymorphic relationship (Many instances of a model called Package (morphedByMany) can contain many different content types (each MorphedToMany).

I’ve created a pivot table containing some additional fields that I’ll need to access and query by, and so I’ve decided that the best thing would be to create a Pivot model by extending the MorphPivot.

Querying is now easy enough, however, I can’t access the content through a relation (which I can do if i query the AppPackages::findOrFail(1)->contentType()). I know I should declare that the pivot belongsTo the contentType, but I’m not sure how to go about it seeing as it could belong to any of the morphed contentTypes.

EDIT
Code blocks as requested

Content1

class Song extends Model
{

  public function packages()
  {
      return $this->morphToMany('AppPackage', 'packageable')->withPivot('choice')->using('AppPackageContent');
  }

Content2

class Video extends Model
{

  public function packages()
  {
      return $this->morphToMany('AppPackage', 'packageable')->withPivot('choice')->using('AppPackageContent');
  }

Package

class Package extends Model
{
  public function songs()
  {
      return $this->morphedByMany('AppSong', 'packageable')->withPivot('choice')->using('AppPackageContent');
  }

  public function videos()
  {
      return $this->morphedByMany('AppVideo', 'packageable')->withPivot('choice')->using('AppPackageContent');
  }

MorphPivot

use IlluminateDatabaseEloquentBuilder;
use IlluminateDatabaseEloquentRelationsMorphPivot;

class PackageContent extends MorphPivot
{
  protected $table = 'packageables';

Pivot table migration

    public function up()
    {
      Schema::create('packageables', function (Blueprint $table) {
        $table->integer('package_id');
        $table->integer('packageable_id');
        $table->string('packageable_type');
        $table->integer('choice');
      });
    }