Pedro Pedro - 4 months ago 55
PHP Question

Multidimensional form array with Vuejs and Laravel 5

I am posting an array of form data using VueJs, this is what it looks like:

hives: [{
hive_type_id: '',
quantity: '',
yard_id: this.yardId,
action: 'added',
added_from: '',
converted_to: '',
notes: '',
logged_at: moment().format('DD-MM-YYYY'),

1) How do I access this in my controller to store to the DB?
I have tried, using a foreach:

foreach($request->get('hives') as $key => $val) { ... }

And then accessing each individual hive data:


But am having no luck storing to the DB.

2) How would I validate this array using a Form Request?

Thanks for you help!


You can drop the $key and give a more meaningful name to $val. Just access it directly, like this:

foreach ($request->get('hives') as $hive) {
    $new_hive = new Hive;
    $new_hive->quantity = $hive->quantity;
    $new_hive->action   = $hive->action;

Form Request validation is well covered in the documentation: