Cleaning Up Form Input with Transpose
Dealing with arrays in form submissions is a pain in the ass.
Imagine you need to build a page that allows users to add multiple contacts at once. If a contact has a name, email, and occupation, ideally the incoming request would look something like this:
[ 'contacts' => [ [ 'name' => 'Jane', 'occupation' => 'Doctor', 'email' => 'firstname.lastname@example.org', ], [ 'name' => 'Bob', 'occupation' => 'Plumber', 'email' => 'email@example.com', ], [ 'name' => 'Mary', 'occupation' => 'Dentist', 'email' => 'firstname.lastname@example.org', ], ], ];
The problem is that crafting a form that actually submits this format is surprisingly complicated.
Originally published at www.laravelfeed.com.