2017-04-25 77 views
1

我想命令Laravel 5.4中的morphTo關係的結果。下面的例子不起作用。在其他關係(例如hasMany())上添加orderBy方法確實有效。Laravel 5.4 orderBy上morphTo關係

class OrderLineItem extends Model 
{ 
    public function eventtable() 
    { 
     return $this->morphTo()->orderBy('date'); 
    } 
} 

我已經能夠通過在集合上使用sortBy對查詢後的結果集進行排序。但是在查詢中排序結果會很好。 date col總是在多態相關表中可用。

回答

0
class OrderLineItem extends Model 
{ 
    public function eventtable() 
    { 
     return $this->morphTo()->orderBy('date','Desc'); 
     //or you can use 
    //return $this->morphTo()->latest('date'); 
    } 
} 
+0

謝謝,但這是行不通的。 – Raiserweb