0

I am trying to remove user IDs in a column for this table called Offer. User IDs are inserted in as a string like '1,2,3,4'. I have a select multiple which contains a list of approved users for the offer in the table. I am having trouble removing an array of userId's from this column field. My code for the function is below:

Public function removeAccess(Offer $offer, Request $request)
{
    $this->validate($request, [
        'private_access' => 'required',
    ]);

    $current_allowed = array($offer->private_allowed);
    $remove_allowed = $request->private_access;
    $allowed = array_diff_key($current_allowed, array_flip($remove_allowed));
    $offer->private_access = $allowed;
    $offer->save();

    return redirect()->back()->with('status', 'Publisher(s) successfully removed from private access.');
}

What am I doing wrong? I thought I would do a loop of each of the $request->private_access and then check if it's in the array, if it is, deletes it. I found another code elsewhere online to do it how I have it displayed, but still running into issues with it deleting all of them even if I select only 1 from the < select multiple > input.

Question Solved! After more research and another 24 hours of on-and-off figuring this out, below is the function now that works for the task.

Important for anyone encountering the same issue, in the select, I had to add [] behind the name-"name[]" to put the values into an array> I also had to convert my array in the database to be similar to the array the multiple select comes with.

    $this->validate($request, [
        'private_access' => 'required',
    ]);

    $current_allowed = explode(',', $offer->private_access);
    $remove_allowed = $request->private_access;
    $filtered = array_diff($current_allowed, $remove_allowed);
    $allowed = implode(',', $filtered);
    $offer->private_access = $allowed;
    $offer->save();

    return redirect()->back()->with('status', 'Publisher(s) successfully removed from private access.');
}
return redirect()->back()->withErrors('An error has occurred.');
IGP
  • 14,160
  • 4
  • 26
  • 43
  • I'm confused so correct me if I'm wrong. So you have a table name `Offer` and there is a column that contains the `user ids` which means that for every row there is a `userid` column that contains `1,2,3,4` as a string like `| 1 | 1,2,3,4 |` – xenooooo Dec 19 '22 at 01:42
  • @xenooooo - Correct! – Jarrod Estepp Dec 19 '22 at 01:45
  • Also does the user can also be in the other offer ? – xenooooo Dec 19 '22 at 01:47
  • @xenooooo - This code is for an offer manage page. This is where an admin can remove currently allowed users to the individual offer. – Jarrod Estepp Dec 19 '22 at 01:51
  • _"User IDs are inserted in as a string like '1,2,3,4'."_ - and _that_ is something you should not have done in the first place; see [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/q/3653462/1427878) – CBroe Dec 19 '22 at 08:31

1 Answers1

0

i think your code must like this

Public function removeAccess(Offer $offer, Request $request) {
    $this->validate($request, [
       'private_access' => 'required',
    ]);
    //make current allowed from db an array
    $current_allowed = explode(",",$offer->private_allowed);
    $remove_allowed = $request->private_access;
    //we must also flip array from database then compare with array_diff_key
    $allowed = array_diff_key(array_flip($current_allowed), array_flip($remove_allowed));
    $offer->private_access = $allowed;
    $offer->save();
    return redirect()->back()->with('status', 'Publisher(s) successfully removed from private access.');
    }
dedy
  • 36
  • 5