Php 第三个表中的Laravel返回属性

Php 第三个表中的Laravel返回属性,php,laravel,laravel-4,Php,Laravel,Laravel 4,我有三张桌子: products product_types product_categories 产品属于产品类型,产品类型属于产品类别 如何从产品中的产品类别访问列: 产品类型型号: class ProductTypes extends Eloquent { public function category() { return $this->belongsTo('ProductCategories'); } } class Product ex

我有三张桌子:

products
product_types
product_categories
产品
属于
产品类型
产品类型
属于
产品类别

如何从
产品
中的
产品
类别访问列:

产品类型型号:

class ProductTypes extends Eloquent {

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

}
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 : '');
    }

}
产品型号:

class ProductTypes extends Eloquent {

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

}
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 : '');
    }

}
我试过:

$productData->types()->category()->category
但这会导致一个错误,即该方法不存在


很抱歉,我想不出标题。

问题是,您在执行
types()
时没有执行查询,因此您在查询生成器实例上调用的是
category()
,而不是
ProductTypes
模型

您可以使用动态属性访问关系的结果

$productData->types->category->category

<>也考虑重新命名你的关系。例如,目前您有“类型”,但它只返回一个类型,因为它是一对多关系。“类型”更有意义