Laravel : Many to many insertion

后端 未结 1 1763
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 12:48

I have two models, User and Team The relationship between them is ManyToMany:

In User:

public fun         


        
相关标签:
1条回答
  • 2021-01-01 13:06

    You should pass an array of user IDs to the attach() method.

    For convenience, attach and detach also accept arrays of IDs as input

    Change your code to:

    $team = \App\Team::findOrFail($request->team_id);
    $team->teamMembers()->attach($request->members_id);
    
    0 讨论(0)
提交回复
热议问题