$allUser = [];
foreach(entry_users as user){
$allinterests[] = [
'name' => $user->name,
'phone' => $user->phone,
];
}
// insert many rows
User::insert($allUser);
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 = AppTeam::findOrFail($request->team_id);
$team->teamMembers()->attach($request->members_id);
/*
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
*/
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
/*To determine the table name of the relationship's intermediate
table, Eloquent will join the two related model names in
alphabetical order. However, you are free to override this
convention. You may do so by passing a second argument to the
belongsToMany method*/
return $this->belongsToMany(Role::class,'role_user');
}
}
//Defining The Inverse Of The Relationship
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany(User::class);
}
}
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
Suppose you have a Post model with a hasMany relationship with Comment. You may insert a Comment object related to a post by doing the following:
$post = Post::find(1);
$commentToAdd = new Comment(['message' => 'This is a comment.']);
$post->comments()->save($commentToAdd);