I've made a method to copy branch user permissions. Permissions usually 0 to 120 items. And running this method to insert this (0 to 120) items to 7 user_ids tooks 35-55 seconds to insert to the table.
/**
* Copy Branch User Permissions
*
* @param Int
* @param Int
* @param Int
* @return Array
*/
public function copyBranchUserPermissions($branchIdFrom, $branchIdTo, $userId)
{
$permissions = BranchPermissionUser::get()
->where('branch_id', $branchIdFrom)
->where('user_id', $userId);
foreach($permissions as $permission) {
$permission->create([
'branch_id' => $branchIdTo,
'permission_id' => $permission->permission_id,
'user_id' => $userId
]);
}
return $permissions;
}
I need to optimize this process because of slow performance in the insertion of data. I'm thinking what if more than 10 users, so this process will be take too long. Is there any way to increase the performance speed in inserting data?
Thank you in advance!