0

I am trying to retrieve an array using axios to do the ajax request, but the result is always empty. Funny is that same request works just fine with artisan tinker!

Let's see some code:

route/web.php file

Route::get('/trocaCarros/{id}', 'VitrineController@trocaCarros')->name('trocaCarros');

The Vuejs method:

methods: {
umaMarca() {
// alert(marca.value);
axios.get('/trocaCarros/' + marca.value).then(response => this.vitrine = response.data);
  }

The VitrineController.php

public function trocaCarros(Request $request)
{
$marca = $request->id;
$umaMarcaModelo = Modelo::where('marca_id','=', $marca);
return response()->json($umaMarcaModelo);
} 

The browser response is:

{}

But, when I did it at the Tinker:

Psy Shell v0.8.11 (PHP 7.1.6 — cli) by Justin Hileman
$marca = 212
=> 212

Then...

$modelo = App\Modelo::where('marca_id','=', $marca)->get();

It returns 6 results, as I have in database, like:

=> Illuminate\Database\Eloquent\Collection {#994
 all: [
   App\Modelo {#995
     id: 880011,
     descricao: "Accord",
     marca_id: "212",
     categoria_id: "1",
     ativo: 1,
     created_at: "2017-08-03 18:35:19",
     updated_at: "2017-08-03 19:41:36",
   },
   App\Modelo {#996
     id: 880012,
     descricao: "City",
     marca_id: "212",
     categoria_id: "1",
     ativo: 1,
     created_at: "2017-08-03 18:47:26",
     updated_at: "2017-08-03 18:47:26",
   },

... and more 4 others

Does anyone know what I missed?

2
  • 1
    Forgot the ->get() in the first one? Commented Oct 17, 2017 at 21:37
  • Yes, i Forgot. Thakns Commented Oct 27, 2017 at 11:50

2 Answers 2

1

you have to get the param from the url in your controller. update your The VitrineController.php like blow.

public function trocaCarros($id, Request $request)
{
    $marca = $id;
    $umaMarcaModelo = Modelo::where('marca_id', $marca)->get();
    return response()->json($umaMarcaModelo);
} 
Sign up to request clarification or add additional context in comments.

1 Comment

OK, now I want to render the result on a DIV. I know how to do it using jQuery, but how AXIOS handles that?
0

When axios, you can get data by using:

axios.post(url,params)
.then(function(response) {
   // response.data contains the data
}).catch(function(error) {

});

Comments

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.