Zlautumn
Zlautumn

Reputation: 99

Accessing values from other tables, Eloquent Laravel

In short, let's say I have several models: Driver [id, name], Cars [id, car_brand_id, model, driver_id], CarBrands [id, name].

So in my drivers.index I want to list all the drivers with their cars, of course stating the car brand name. I have also stated all the needed relations within the models.

If I retrieve it like $drivers = Drivers::with('cars')->get(), how can I then access the name of its brand in the view?

Upvotes: 1

Views: 134

Answers (1)

Rafal Migda
Rafal Migda

Reputation: 674

You can go deeper and deeper with relations. Eg.:

$drivers = Drivers::with(['cars.brand'])->get()

Upvotes: 2

Related Questions