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' => 'email@example.com', ], [ 'name' => 'Bob', 'occupation' => 'Plumber', 'email' => 'firstname.lastname@example.org', ], [ 'name' => 'Mary', 'occupation' => 'Dentist', 'email' => 'email@example.com', ], ], ];
The problem is that crafting a form that actually submits this format is surprisingly complicated.
Originally published at www.laravelfeed.com.