2015-04-27 235 views
1

我有一个有许多Action的Objective模型,每个Action都有一个ActionYear。已经在模型中定义。Laravel雄辩的关系orderby问题

如何使用orderby通过action_year的特定列对目标中的操作进行排序。

$query = Objective::with('actions.actionYear') 
      ->orderBy('action_year.created_at') 
      ->get(); 

这通过错误Undefined table: 7 ERROR: missing FROM-clause entry for table "action_year"。 如何解决这个问题。谢谢。

回答

0

如果使用()函数,那么这只能确保指定的关系也被加载,以避免以后对SQL查询的额外需求。 Othewise它相比Objective::all()

一种方法如何实现有序集合不引入任何附加的变化是使用sortBy()功能加载这样

$query = $query->sortBy(function($objective){ 
    return $objective->actionYear->created_at; 
}); 
1

数据后,您可以用封闭令渴望加载模型:

$query = Objective::with(['actions.actionYear' => function($q){ 
    $q->orderBy('action_year.created_at'); 
})->get(); 
+0

仍然有同样的问题。 –

+0

但即使是右桌也是'action_year'? – lukasgeiter

+0

是'action_year'。我有三层 - 客观 - >行动 - > action_year' –