Skip to content
Advertisement

How to get an attribute throw a table in Eloquent?

I have requests table that belongs to subcategories table which belongs to categories table, so from Request model how can I get an attribute from categories?

JavaScript

So I need to get color from Request model, I already have the tree models created

JavaScript

Advertisement

Answer

You need to define your relationships:

JavaScript

JavaScript

Then you could just do:

JavaScript

Of course, you can add a computed attribute to your Request class to easy access (keep in mind that this could increase the number of queries):

JavaScript

Then:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement