我有一个model
与具有这种命名的关系:Laravel 4个关系与名称由undescore分离不工作
class PurchaseOrder extends Eloquent implements IModel
{
protected $guarded = ['id'];
protected $table = 'purchase_orders';
// this function has name separated by an _ or underscore
public function purchased_items()
{
return $this->hasMany('PurchasedItem');
}
}
,我使用它访问:
$posted_po = PurchaseOrder::find($po_id);
$purchased_items = $posted_po->purchased_items->all();
上面的代码产生错误
PHP Fatal error: Call to a member function all() on a non-object
但以某种方式更改关系的名称lves我的问题:
public function purchasedItems()
{
return $this->hasMany('PurchasedItem');
}
$posted_po = PurchaseOrder::find($po_id);
$purchased_items = $posted_po->purchasedItems->all();
现在,我的问题是,为什么会发生这种情况?这种行为背后的任何理由?
我想这可能是因为你把它叫做属性而不是方法。尝试在方法名称后添加正常大括号,所以它就像'$ posted_po-> purchased_items() - > all()' – NorthBridge 2014-10-31 03:22:00