【发布时间】:2014-11-11 19:08:59
【问题描述】:
我有两张桌子,一张叫"products",另一张叫"product_brands"。
一个产品有一个品牌,一个品牌可以属于多个产品。
我有:
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_brands" 中的列与 "products" 中的列位于同一数组中,因此我尝试使用 toArray() 和 getBrandAttribute() 进行这些操作,但是它不工作。
我该怎么做?
【问题讨论】:
标签: php mysql laravel laravel-4