【发布时间】:2015-03-13 14:35:28
【问题描述】:
我正在尝试将现有的 Laravel 4 项目升级到版本 5。
模型关系无法正常工作。每次我尝试从 property_price 表访问属性时,它都会返回 null。
我的模型位于App/Models 目录中。
属性模型
class Property extends \Eloquent {
protected $guarded = array('id');
protected $table = 'properties';
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $softDelete = true;
public function propertyPrice()
{
return $this->hasOne('PropertyPrice','pid');
}
}
PropertyPrice 模型
class PropertyPrice extends \Eloquent {
protected $guarded = array('id');
protected $table = 'property_pricing';
public function property()
{
return $this->belongsTo('Property');
}
}
用法
$property = Property::find($id);
$price = $property->property_price->per_night_price; // null
代码在 Laravel 4 中运行良好。
【问题讨论】:
-
你的模型有命名空间吗?