2

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();
}
1
  • you actually have done it .. Commented May 26, 2017 at 2:10

1 Answer 1

2

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"]

Sign up to request clarification or add additional context in comments.

9 Comments

My bad, the axios call converts the array to string. However I do have to convert it to an array if I want to use it as I want: $register = Ruta::where('CODIRUTA','=',$rutas[$index])->first();. My need to pass it as an array instead of a string remains. Offcourse, I could just convert it, but I want to know if it's possible.
since your rutas is an array in my answer .. you can retrieve a Ruta using $register = Ruta::whereIn('CODIRUTA',$rutas)->first();
If I run that code with an array ["01", "02"] I will get the first one every time, I don't need to do the query with an array of ids ($rutas). I need to loop the $rutas and do the query individually so I can get a different "ruta" each time. Also, $request->rutas does not return an array ["01", "02"], it returns a string "01,02". Thanks for your help though.
it is because you're converting it .. use params.append("rutas", $rutas); instead of params.append("rutas", $strRutas);
I'm working with params.append("rutas", $rutas); since you posted your answer. I'm not converting it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.