,我已經試過各種方法,但我不獲得期望的結果laravel父的關係屬於關聯
class Product extends \Eloquent {
public function producttype() {
return $this->belongsTo('ProductTypes', 'producttype_id');
}
}
class ProductTypes extends \Eloquent {
public function products() {
return $this->hasMany("Product", 'id');
}
}
$product->producttype->title
更新
class ProductTypes extends \Eloquent {
protected $fillable = ['keywords', 'description', 'title', 'slug'];
protected $table = "producttypes";
public function products() {
return $this->hasMany("Product", 'product_id', 'id');
}
}
class ProductVariations extends \Eloquent {
protected $table = "productvariations";
protected $fillable = ['product_id', 'producttype_id', 'price', 'quantity', 'discount', 'image'];
public function product() {
return $this->belongsTo('Product');
}
}
public function show($id)
{
$product = Product::with('producttype')->findOrFail($id)->get();
return View::make('products.show')->withProduct($product);
}
顯示頁面
$product->producttype->title
顯示你的表。 – 2014-10-09 06:31:39