Adam Allen
Adam Allen

Reputation: 208

Retrieving an Array with Axios in Laravel

i am making an Axios request to one of my controller functions which is going into my DB and getting all the suppliers information however when it is return to my VUE file there is no data

This is my request:

mounted(){
            axios.get('/product/'+ this.url + '/suppliers/info').then(response=>console.log(response.data));

        },

and this is my controller function

public function getSuppliers($product){

        $suppliers = $this->supplier->getAll();

        return response()->json($suppliers);
    }

When i dd out $suppliers in the controller it has all the data so i know it has been success retrieved from the DB but when i console log it out once it has been return to VUE the data is gone.

This is what i see if i just console log response:- enter image description here

I can see it is return all the arrays which should be there but none of them have any data in? what am i doing wrong?

This is a snippet of $suppliers when i DD it in the controller: enter image description here

Image of the request header from network:- enter image description here

getAll function :-

public function getAll()
{
    return $this->buildEntities($this->data->orderBy('name')->get());

}

Upvotes: 0

Views: 1553

Answers (1)

LorenzoBerti
LorenzoBerti

Reputation: 6974

Your $suppliers doesn't seems an array, but a eloquent model. so try this:

return response()->json(["suppliers"=>$suppliers], 200);

And see in response.data.suppliers

** EDIT **

The problem can be the function buildEntity. try to

$this->data->orderBy('name')->get()

Upvotes: 2

Related Questions