How to efficiently update/sync pivot table/join table using PHP with selected multiple checkbox

First, Let’s assume we pass selected multiple checkboxes ids to parameter permissionIds, eg. 4,5,2,12,9

$existsPageIdsAry = array_column($existsPermissions, 'pageID');

$permissionIdsAry = explode(',',$data['permissionIds']);

$pageIdsNeedRemove = array_diff($existsPageIdsAry, $permissionIdsAry);

$pageIdsNeedAdd = array_diff($permissionIdsAry, $existsPageIdsAry);

Then we can do delete where id in ($pageIdsNeedRemove) and insert pivot table records in $pageIdsNeedAdd

Leave a Reply

Your email address will not be published. Required fields are marked *