2017-08-24 5 views
1

Ich bin neu auf Laravel 5.4 und Vue js 2 und ich bin konfrontiert mit dieser Frage, die ich lösen kann.Laravel 5/Vue JS: Route für Get Anfrage Route zurück Object oder Array abhängig Eloquent Abfrage

Ich habe einen Controller mit 2 Methoden bekommt (und Route zugeordnet):

public function index() 
{ 
    // 
    return BookingPayment::all(); 
} 

public function index_booking($id_booking) 
{ 
    return BookingPayment::all() 
     ->where('id_booking', '=', $id_booking) 
     ->toArray(); // <<-- no effects !? 
} 

Methode index alle Zeilen in meinem Tisch zurückkehren, index_booking id_booking Felder einen Filter tun.

In PHP meine PHP-Seite Ich habe ein Javascript, das eine Anfrage auf diesen beiden Routen. Mein Problem ist, wenn ich index_booking verwenden, Anfrage gibt mir ein Objekt zurück. Ich erwarte ein Array, da ich die Array-Funktion einfach benutzen kann, um die Ergebnisse zu manipulieren.

Hier ist mein javascript:

var vm = new Vue({ 

el: '#bookingpay', 

data: function() { 
    return { 
     booking_id: 15, 
     payments: [], 
     input: [{id: 0, id_booking: 0, new_amount: 99}] 
    } 
}, 

mounted: function() { 
    this.fetchPayment(); 
}, 

methods: { 

    fetchPayment: function() { 
     this.$http.get('/api/bookingpayments_bybooking/' + this.booking_id).then(function (response) { 
      this.payments = response.data; 
     }) 
    }, 

    delPayment: function (id, index) { 
     this.$http.delete('/api/bookingpayments/' + id).then(function() { 
      this.payments.splice(index, 1); // doesn't work on Ojbect 
     }) 
    }, 

    addPayment: function() { 
     this.$http.post('/api/bookingpayments', this.input).then(function() { 
      this.payments.push(this.input); 
     }) 
    }, 

} 

}); 

Dies ist die Art von Format, die ich als eine http-Anfrage-Antwort erwarten. Es ist, was ich habe, wenn ich Index verwenden: [ { "id": 79, "id_booking": 3, "amount": "10.00" }, { "id": 80, "id_booking": 3, "amount": "10.00" }, ....

Dies ist, was ich habe, wenn ich index_booking verwenden: { "28": { "id": 29, "id_booking": 15, "amount": "45.00" }, "29": { "id": 30, "id_booking": 15, "amount": "48.00" } } Ich bin fest! Jede Hilfe wird wirklich geschätzt. Prost

+0

Ist 'BookingPayment' ein beredtes Modell? –

+0

ja ist es! Ian hat mein Problem gelöst. trotzdem danke – leyom

Antwort

0

Ihre index_booking ist falsch. Der richtige Weg ist:

public function index_booking($id_booking) 
{ 
    // This will return a Collection that will be translated to an array/json when you hit the endpoint via ajax. There is no need for toArray(), though... 
    return BookingPayment::where('id_booking', '=', $id_booking)->get(); 
} 
+0

Das ist mein Problem zu lösen. Danke für deine Hilfe, Kumpel! – leyom

+0

Großartig! Jederzeit! ps: +1 Kredit auf die Antwort –