[Vuejs]-Nuxt Axios post to Laravel API with multiple parameters

0👍

can validate and post like this

$this->validate($request, [
        'form.first_name' => 'required',
        'form.last_name' => 'required',
        'form.address_1' => 'required',
        'form.address_2' => 'required',
        'form.city' => 'required',
        'form.postcode' => 'required',
        'form.country_id' => 'required|exists:countries,id',
    ]);

    $address = new Address;
    $address->first_name = $request['form.first_name'];
    $address->last_name = $request['form.last_name'];
    $address->address_1 = $request['form.address_1'];
    $address->address_2 = $request['form.address_2'];
    $address->city = $request['form.city'];
    $address->postcode = $request['form.postcode'];
    $address->country_id = $request['form.country_id'];
    $address->save();

but can also use the spread operator …this.form and wouldn’t need to access them like the above in the controller

enter async postOrder () {
  try {
     await this.$axios.$post('orders',{
      ...this.form,
      shipping_id: this.$store.state.shipping.id,
    })

  } catch (e) {

  }

},

0👍

You are posting data wrapped in a form object, so you have to change your validation rules to:

$this->validate($request, [
    'form.first_name' => 'required',
    'form.last_name' => 'required',
    etc...

You should also checkout your console to see the catched error exception from the try/catch in your Nuxt app.

Leave a comment