Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Laravel中的数据库获取不同的属性_Php_Mysql_Laravel_Laravel 4 - Fatal编程技术网

Php 从Laravel中的数据库获取不同的属性

Php 从Laravel中的数据库获取不同的属性,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,我有两个表,一个叫做“产品”,另一个叫做“产品品牌” 一个产品有一个品牌,一个品牌可以属于多个产品 我有: class Product extends Eloquent { protected $table = 'products'; public function type() { return $this->hasOne('ProductTypes'); } public function brand() { r

我有两个表,一个叫做
“产品”
,另一个叫做
“产品品牌”

一个产品有一个品牌,一个品牌可以属于多个产品

我有:

class Product extends Eloquent {
    protected $table = 'products';
    public function type() {
        return $this->hasOne('ProductTypes');
    }

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

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

    public function toArray() {

        $ar = $this->attributes;

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

        return $ar;
    }

    public function getBrandAttribute() {
        $brand = $this->brand()->first();
        return (isset($brand->brand) ? $brand->brand : '');
    }
}
和我的控制器:

class ProductsController extends BaseController {

    public function index($type_id) {
        $Product = new Product;
        $products = $Product->where('type_id', $type_id)->get();
        return View::make('products.products', array('products' => $products));
    }

}
理想情况下,我希望
中的“product\u brands”
列与
中的“products”
列位于同一个数组中,因此我尝试使用
toArray()
getBrandAttribute()
的方法,但它不起作用


我如何才能做到这一点?

我确信
getBrandAttribute
访问器与
brand
关系冲突。请尝试以下方法:

class Product extends Eloquent {
    protected $table = 'products';
    public function type() {
        return $this->hasOne('ProductTypes');
    }

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

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

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

    protected $appends = array('brand'); // this makes Laravel include the property in toArray

}

您应该将访问者更改为其他名称:

public function getSpecBrandAttribute() {
    $brand = $this->brand()->first();
    return (isset($brand->brand) ? $brand->brand : '');
}
然后在
toArray
中,您应该使用:

public function toArray() {

    $ar = $this->attributes;

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

    return $ar;
}
这是因为您不应该创建与关系名称同名的字段

此外,由于这是一对多关系,可能对于
brand()
您应该使用
belongsTo
而不是
hasOne