Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ivan2nn's avatar

how to use vuew resource sending an array of strings

I am not able to pass an array of strings through an ajax call to a Route.

The route definition is:

Route::get('api/taxonomytospecies/{ids}', 'TaxonomyController@getSpeciesFromTaxonomy');

The vue defintion is:

var data = { ids : checked_leaves };
        console.log(this.el.action);

    this.vm.$http.get(this.el.action+'/:ids', {ids : 1}).then((response) => {
        // Inside the response data there are also the taxonomy data, but the google map API cna distinguish by itself
        console.log(JSON.parse(response.data));
        //this.vm.speciesDetails = JSON.parse(response.data);
        //this.vm.loading = false;
    }, (response) => {

    });

The controller method is:

public function getSpeciesFromTaxonomy(Request $request) {
    return json_encode($request->get('ids'));
}

In the HTML I have

<form method="GET" action="api/taxonomytospecies" v-ajax>
.....

The array i should wens id checked_leaves, but I am making a simpler attempt just to receive what i send through vue-resource and the response.data is always null

0 likes
4 replies
willvincent's avatar

I don't think your request makes sense here, nor do I think you can really pass an array as a route parameter. You could send it as a get parameter to a route, but then that route shouldn't be expecting a param.

this does not make sense: this.vm.$http.get(this.el.action+'/:ids', {ids : 1})

And, if you pass a get param with the same name as a named route parameter that's likely to be troublesome as well.

ivan2nn's avatar

Thnak you. But I don't get where the error exactly is. I am setting ids to 1 becasue I am making a test (actually I would like to send an array of string to an endpoint so that in that endpoint I can make a retrieve from the db of each value in the array).

But the action is the URL where i should make the request right? So the problem is how i configure the parameter in the Route file or is how i call the http.get in the Vue file?

prasinoulhs's avatar
Level 4

Change your route to Route::get('api/taxonomytospecies/', 'TaxonomyController@getSpeciesFromTaxonomy'); then use the params option to pass the array.

this.vm.$http.get(this.el.action, {params: { ids: [....] } })
ivan2nn's avatar

Thank you prasinoulhs, obviously it worked.

If you have one minutes could you explain what is a "parameter" in this case, and why i cannot set a route GET with a value like in the definition of resources (I know that in Laravel i can set a GET like user/show/{id} to be a user/show/{user} so to receive an object.

Why can't i do the same (like i tried to do) with a generic route?

Even if you point me to a useful link I will be grateful.

Thank you

Please or to participate in this conversation.