我已经建立了一个很好的客户关系管理系统,在那里我与组织建立了联系
在我的接触模型中我有:
public function organizations()
{
return $this->belongsToMany('Organization')
->withPivot('ContractStatus')
->withTimestamps()
->orderBy('created_at', 'desc');
}
问题是,它现在正在组织表的创建日期上排序。但是,为了响应每个联系人的作业历史列表,我希望在pivot表的created_at字段上订购,因为这是contact en Organization之间的合同签订日期。
我该怎么做?
发布于 2014-09-26 10:44:07
问题是,关系上的透视表字段使用前缀pivot_
进行别名,因此它们不会与相关的表字段发生冲突。
话虽如此,你需要这样做:
public function organizations()
{
return $this->belongsToMany('Organization')
->withPivot('ContractStatus')
->withTimestamps()
->orderBy('pivot_created_at', 'desc');
}
请注意,这只在使用withTimestamps()
时才能工作,否则您将得到unknown column
db错误。
编辑:根据OP注释,您可以始终使用传统方法,并在字段前面加上支点表名:
public function organizations()
{
return $this->belongsToMany('Organization')
->withPivot('ContractStatus')
->withTimestamps()
->orderBy('PIVOT_TABLE.created_at', 'desc');
}
https://stackoverflow.com/questions/26057196
复制相似问题