Hi, I'm trying to get services by using ->with, but it doesn't load the services. I have on my Booking model a belongsToMany relationship.
So this is what I'm doing, I have a VUE search input which looks up Bookings by there booking ID, and that's open a link using window.href.
Here is my code:
MODEL
public function services()
{
return $this->belongsToMany('App\Service');
}
CONTROLLER
public function search(Request $request)
{
$booking = Booking::where('id', '=', $request->bookingId)->with('services')->first();
return $booking;
}
SEARCHBOOKING.VUE
<template>
<div>
<input class="appearance-none block w-full bg-gray-300 text-gray-700 border border-gray-200 rounded py-2 px-4 focus:outline-none focus:bg-white focus:border-main transition" type="number" placeholder="Leita af bókun" v-model="bookingId" @keyup.enter="search">
</div>
</template>
<script>
export default {
data() {
return {
user: window.App.user,
bookingId: ''
}
},
methods: {
search: function (e) {
e.preventDefault();
e.stopPropagation();
axios.post('/api/booking/search?api_token=' + this.user.api_token, {
bookingId: this.bookingId
})
.then(response => {
window.location.href = "/stjornbord/bokanir/" + response.data.id;
})
.catch(function (error) {
console.log(error);
});
}
},
};
</script>
ROTUES
Route::post('/booking/search', 'BookingController@search');
Route::get('/stjornbord/bokanir/{booking}', 'DashboardController@showBooking');