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
Ist 'BookingPayment' ein beredtes Modell? –
ja ist es! Ian hat mein Problem gelöst. trotzdem danke – leyom