我第一次尝试使用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');
}Shipment_question:
public function Shipment()
{
return $this->belongsTo('Shipment','shipment_id');
}下面是我们的号召:
Auth::User()->Shipment_question;我使用barryvdh调试器来查看查询,shipment_question调用应该生成一个查询,但是在查询日志中只显示auth::user查询。
发布于 2014-09-21 15:43:44
为了作为动态属性工作,关系必须是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;一般而言,使用camelCased StudlyCased方法和属性以及StudlyCased类的Laravel4+命名约定(如上面所示,或至少在其他情况下建议使用)。
因此,我建议将Shipment_question类重命名为ShipmentQuestion。
https://stackoverflow.com/questions/25960552
复制相似问题