php - associate and attach not working? -
i have 2 models : user , favoriteuser
my relationship looks :
user:
public function favorites(){ return $this->hasmany('app\models\favoriteuser', 'user_id'); }
favorite user:
public function favorite_users() { return $this->belongsto('app\models\user'); }
so how can this:
$user->favorites()->associate($user_id);
you need have 2 model user , favorite
and declare in user:
public function favorites() { return $this->belongstomany('app\models\favorite''); }
and in favorite:
public function users() { return $this->belongstomany('app\models\user'); }
and can do:
$user->favorites()->attach($user_id);
Comments
Post a Comment