jeudi 25 février 2016

Laravel Eloquent attaching to pivot table

I have a Product Model and an Adjustment Model as Pivot.

So when I make a Product change it should be saved in the pivot table.

That´s the pivot table

   $table->increments('id');
   $table->integer('product_id')->index()->unsigned();
   $table->text('before');
   $table->text('after');
   $table->timestamps();

That´s the boot and the adjustments Relationship

 public static function boot()
{
    parent::boot(); // TODO: Change the autogenerated stub

    static::updating(function ($product) {
        $product->adjustments()->attach([
                'before' => $product->fresh()->toJson(),
                'after' => json_encode($product->getDirty()),
            ]
        );
    });
}


public function adjustments()
{
    return $this->belongsToMany(Adjustment::class, 'adjustments', 'product_id')->withPivot(['before', 'after'])->withTimestamps();
}

That´s the error

Column not found: 1054 Unknown column 'adjustment_id' in 'field list' (SQL: insert into `adjustments` (`adjustment_id`, `after`, `before`, `created_at`, `product_id`, `updated_at`) values ...

I have the code from laracasts.com I dont know how it needs an adjustment_id ? How can you fix that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire