Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LonnieD's avatar

Creating array of IDs from HTML form array for detach() method

Hello all!

I am attempting to use the detach() method on a many-to-many pivot table and having trouble creating an array of IDs to send to the method.

From the Laravel 5.8 documentation:

For convenience, attach and detach also accept arrays of IDs as input:

$user = App\User::find(1);

$user->roles()->detach([1, 2, 3]);

My HTML form is sending the correct array of checkbox values as the following:

array:1 [ "input" => array:3 [ 0 => "562" 1 => "570" 2 => "574" ] ]

My question is, how do I make a new array from the values of my form input to send to the detach() method?I have tried all kinds of loops and array functions but always end up with a new array with key=>value pairs. According to the documentation, I need an array that looks like this:

detach([562,570,574]);

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

[ 0 => 562, 1 => 570, 2 => 574 ]

is the same as

[562, 570, 574]

If you have an associative array with consecutive integer keys starting at 0, then you have a normal array

LonnieD's avatar

Thanks so much for the quick reply! I sent the array as-is to the detach() method and the array worked as you said. Now for a refresher in PHP array basics.

tykus's avatar

Great! Please mark it solved.

Please or to participate in this conversation.