0
votes

Tables:

Gift: id, name  
User: id, name  
GiftUser: gift_id, user_id, count  

In GiftUser model, I already have this line

public $timestamps = false;

In migration seeder:

foreach ($all_gifts as $key => $gift) {
  $gift_user = GiftUser::where('gift_id', $gift['id'])->where('user_id', $user->id)->first();

  if(empty($gift_user)){
    $user->gifts()->attach($gift['id'], ['count' => 1]);
  }else{
    $user->gifts()->updateExistingPivot($gift, ['count' => ($gift_user->count+1)]);
  }
}

This foreach is to add a gift_user record. For each gift, if the user doesn't have, add a new record. If the user already has the gift, count+1.

But when executing the seeder, it says

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'field list' (SQL: insert into gift_user ( count, created_at, gift_id, updated_at, user_id) values (1, 2018-04-11 13:49:14, 2, 2018-04-11 13:49:14, 1))

In Connection.php line 452:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'created_at' in 'field list'

When the two columns created_at and updated_at exist, it's ok.
But now I don't want those two columns.

I guess, when using attach() and updateExistingPivot(), GiftUser model seems not used, right? How to fix it?

1
then u have to remove it from table and migration also. or u can send in created_at current timestampKuldeep Mishra
It's already removed from table and migration. The problem is, no such columns, but seeder tries to insert.ronrun
share your seeder codeKuldeep Mishra
I already shared my seeder code when I ask this question!!!!ronrun

1 Answers

0
votes

something its wrong, so my answer is not a solution, just a quickfix (maybe)

try to guard this fields inside a model:

protected $guarded = ['created_at', 'updated_at'];