I have set up a relationship between 2 models Route and Booking
a Route has many bookings a Booking belongs too a route
Route Model:
//
public function books()
{
return $this->hasMany(Booking::class);
}
Booking Model
public function route()
{
return $this->belongsTo(Route::class);
}
Now in my controller i try to access the route a particular booking belongs to
public function bookFeed(Request $request, Booking $booking)
{
$bookR = $booking->with('route')->get();
dd($bookR);
}
or even this
$bookR = $bookRef->route()->get();
dd($bookR);
and i get a collection with multiple Route arrays, now to access a value i have to do
$bookR[0]->value
why is this so, what am i doing wrong?
$booking->load('route');instead of$booking->with('route')->get()This will load the related route of your booking. Your code starts a new query when using like this.