I have project_group
pivot table with this fields: id, group_id, project_id, admin_id, user_id
This I use to attach group and projects together:
$group -> projects() -> attach($projects,array('admin_id' => Auth::user()->id));
Is it possible to for every record in that pivot table add diffirent user_id.
For example:
First record:
id
= 1, group_id
= 1 project_id
= 2 admin_id
= 1 user_id
= 1
Second record:
id
= 2, group_id
= 1 project_id
= 3 admin_id
= 1 user_id
= 1
3th record:
id
= 3, group_id
= 1 project_id
= 2 admin_id
= 1 user_id
= 2
4th record:
id
= 3, group_id
= 1 project_id
= 3 admin_id
= 1 user_id
= 2
Basicly if I select 2 projects from projects html list and 2 users from html users list I need to get result like in example above...
Sure, like this:
$projects = [
2 => ['admin_id' => 1, 'user_id' => 1],
3 => ['admin_id' => 1, 'user_id' => 2],
// and so on
];
$group->projects()->attach($projects);
And if I understood your problem right, you can build such an array like this:
$projectsIds = [2,3];
$userIds = [1,2];
$projects = [];
$adminId = Auth::id();
foreach($userIds as $userId){
$projects += array_fill_keys($projectIds, [
'admin_id' => $adminId,
'user_id' => $userId
]);
}
$group->projects()->attach($projects);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With