How to Pass Post Parameter from Laravel Route to Axios in Vue JS?

Here’s an example, how you can send request data from axios to controller of laravel route.

Axios Call with Vue.js

axios.post('/payments', {
    params: {
        data_from: “test data from”,
        data_to: “test data to”
    }
})

Route:
Route::post(‘/payments’, ‘Api\PaymentController@apiPaymentByUserId’);

Controller:

Class PaymentController extends Controller  {
        public function apiPaymentByUserId(Request $request) { 
            dd($request->all());
        }
 }

We will get below output from dd($request->all());

Output:

array:1 [
  "params" => array:2 [
    "data_from" => "test data from"
    "data_to" => "test data to"
  ]
]

You also can access individual property of request using below code of Controller:

Class PaymentController extends Controller  {
        public function apiPaymentByUserId(Request $request) { 
            $data_from = $request->data_from;
    $data_to = $request->data_to;
        }
 }

 

Subscribe for
weekly updates

newsletter