Laravel从第三张桌子返回财产

I have three tables:

products
product_types
product_categories

products belongs to product_types and product_types belongs to product_categories.

How can I access a column from product_categories from products?:

ProductTypes model:

class ProductTypes extends Eloquent {

    public function category() {
        return $this->belongsTo('ProductCategories');
    }

}

Products model:

class Product extends Eloquent {

    protected $table = 'products';

    public function brands() {
        return $this->belongsTo('ProductBrands', 'brand_id', 'id');
    }

    public function ages() {
        return $this->belongsTo('ProductAges', 'age_id', 'id');
    }

    public function types() {
        return $this->belongsTo('ProductTypes', 'type_id', 'id');
    }

    public function images() {
        return $this->hasMany('ProductImages');
    }

    public function reviews() {
        return $this->hasMany('ProductReviews');
    }

    public function toArray() {

        $ar = $this->attributes;

        $ar['brand'] = $this->brand;
        $ar['age'] = $this->age;
        $ar['type'] = $this->type;

        return $ar;
    }

    public function getBrandAttribute() {
        $brands = $this->brands()->first();
        return (isset($brands->brand) ? $brands->brand : '');
    }

    public function getAgeAttribute() {
        $ages = $this->ages()->first();
        return (isset($ages->age) ? $ages->age : '');
    }

    public function getTypeAttribute() {
        $types = $this->types()->first();
        return (isset($types->type) ? $types->type : '');
    }

}

I have tried:

$productData->types()->category()->category

But this gives an error saying the method doesn't exist.

Sorry about the title, couldn't think of one.

The problem is, that you're not executing the query when doing types() and therefore you're calling category() on the query builder instance and not the ProductTypes model.

You can use the dynamic properties for accessing the result of the relationship.

$productData->types->category->category

Also consider renaming your relationships. Currently you have "types" for example but it only returns one type, because its a one-to-many relation. "type" would make more sense.