我正在尝试第一次使用hasManyThrough关系。但是,当我尝试调用该方法时,它什么也不返回。
这是我的数据库:
User:
- id
- (...)
Shipment:
- id
- users_id
- (...)
Shipment_question:
- id
- shipment_id
- (...)
这是我模型的代码。
用户:
public function Shipment()
{
return $this->hasMany('Shipment','users_id');
}
public function Shipment_question(){
return $this->hasManyThrough('Shipment_question','Shipment','users_id','shipment_id');
}
运输:
public function User()
{
return $this->belongsTo('User','users_id');
}
public function Shipment_question()
{
return $this->hasMany('Shipment_question','shipment_id');
}
发货问题:
public function Shipment()
{
return $this->belongsTo('Shipment','shipment_id');
}
这里是电话:
Auth::User()->Shipment_question;
我正在使用barryvdh调试栏查看查询,shipment_question调用应生成一个查询,但是在查询日志中仅显示auth :: user查询。
为了用作动态属性,关系必须为camelCased
,因此重命名:
public function Shipment_question(){
return $this->hasManyThrough('Shipment_question','Shipment','users_id','shipment_id');
}
//to
public function shipmentQuestion(){
return $this->hasManyThrough('Shipment_question','Shipment','users_id','shipment_id');
}
那么您将可以调用它:
Auth::user()->simpmentQuestion;
否则,使用它的唯一方法就是直接将其作为方法调用:
Auth::User()->Shipment_question()->getResults();
// what gives the same result as dynamic property after fix:
Auth::User()->shipmentQuestion;
通常,Laravel4 +命名约定使用camelCased
方法,属性和StudlyCased
类来强制(如上,或至少对其他情况建议)。
因此,我建议也重命名您的Shipment_question
班级ShipmentQuestion
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句