mercredi 2 août 2017

ArgumentCountError: Too few arguments to function Illuminate\Database\Eloquent\Relations\Pivot::__construct(), 0 passed

I'm using Laravel 5.4.* and I have the following relationship on an Eloquent Model

public function metaData()
  {
     return $this->morphMany(MetaData::class, 'meta');
  }

  public function getMetaDataAttribute()
  {
      return $this->metaData();
  }

public function newPivot(Model $parent, array $attributes, $table, $exists, $using = NULL)
    {
        if($parent instanceof Request ) {
          return new MetaData( $parent, $attributes, $table, $exists, $using);
        }

        return parent::newPivot($parent, $attributes, $table, $exists, $using);
    }

The class Meta is as follows

class MetaData extends Pivot
{
    protected $table = "meta_data";
    protected $primaryKey = null;
    protected $foreignKey = 'meta_id';
    public $timestamps = false;
    public $incrementing = false;
    protected $fillable = [
      'meta_id',
      'meta_key',
      'meta_type',
      'meta_data'
    ];

}

When trying to retrieve the relation via $this->metaData() I get the following error

ArgumentCountError: Too few arguments to function Illuminate\Database\Eloquent\Relations\Pivot::__construct(), 0 passed in /home/vagrant/Code/dohlapse/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php on line 487 and at least 3 expected

Based on the documentation morphMany takes just two arguments which I seem to have fulfilled.

So what is the cause of this error? Should I use a Model instead of Pivot??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire