Reputation: 751
I'm using Laravel 5.4 and I'm handling my data with Vue 2. I'm Trying to call a controller method using axios, but I can't find a proper way to pass two arrays. I need to send them to my controller in order to make some database requests with them and get some data back.
I've manage to pass the arrays as strings as shown below, however, I would prefer not to transform the strings to arrays on my controller.
Could there be a way to do the same thing as I'm doing below but passing the arrays as arrays instead of strings or is this just not possible?
Axios call on Vue:
$rutas = ["01", "02"];
$tipos = ["01", "03", "04"];
var params = new URLSearchParams();
params.append("rutas", $rutas);
params.append("tipos", $tipos);
var request = {
params: params
};
axios.get('/test/', request);
This is my route
Route::get('/test/', 'TestsController@getClientesPorRuta');
This is my controller's method:
public function getClientesPorRuta(Request $request) {
$rutas = $request->input('rutas');
$tipos = $request->input('tipos');
// Code like this doesn't work
$index = 0;
$register = Ruta::where('CODIRUTA','=',$rutas[$index])->first();
}
Upvotes: 2
Views: 2972
Reputation: 1672
actually you didn't have to convert that array to string to do that .. you can directly pass it ..
var request = {
rutas: $rutas,
tipos: $tipos,
};
axios.post('/test/', request);
use axios.post instead of axios.get .. since get method is using the url to pass the data .. and url cant contain array it is converting it to string like 1,2
also in your routes you should change the Route::get to Route::post
Route::post('/test/', 'TestsController@getClientesPorRuta');
then in your php $request->rutas
is already set as ["01", "02"]
Upvotes: 2