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?