Here is my situation: The user can comment on the video. The comment belongs to both the video and the user. My models look like this:
class Comment extends Eloquent { public function video() { return $this->belongsTo('Video'); } public function user() { return $this->belongsTo('User'); } } class User extends Eloquent { public function comments() { return $this->hasMany('Comment'); } } class Video extends Eloquent { public function comments() { return $this->hasMany('Comment'); } }
And I'm trying to insert a comment:
$comment = new Comment; $comment->content = 'content'; Auth::user()->comments()->save($comment);
This causes an Integrity constraint violation error from SQL because it updates only one foreign key. Doing this the other way around (saving video) gives the same result. How to add it to both models at once by updating both foreign keys?
source share