2015-01-09 61 views
0

好日子,如何Laravel使用SUBSTR()的排序依據函數內部

我需要通過使用laravel「排序依據」功能,但我的排序結果不幸的是,我想用在該列中的值「 j1_」實際數字前..所以我想先刪除第一個3個字符,但是當我試圖

orderBy(substr('x_fs_format_details.tree_xid', 3)) 

它給了我一個‘未找到’的錯誤列。

有沒有辦法來調整呢?謝謝。

+0

你需要一個內置SGBD功能來做到這一點。你的SGBD是什麼? – 2015-01-09 20:49:27

+2

你確定你有一列名爲's_format_details.tree_xid'嗎? – BrokenBinary 2015-01-09 20:49:58

+0

這可能有助於http://laravel.io/forum/02-10-2014-orderby-with-my-custom-attributes – litechip 2015-01-09 20:50:02

回答

3

我假設你使用MySQL(子字符串函數可能在其他數據庫不同)

您可以隨時進行新的領域:

$query->selectRaw('*, SUBSTR(x_fs_format_details.tree_xid, 3) AS substr_tree_xid') 
     ->orderBy('substr_tree_xid')->get(); 

這樣做的好處/缺點那SUBSTR的結果將會在你的結果中。如果你不想,你可以在訂單直接還使用SUBSTR

$query->orderByRaw('SUBSTR(x_fs_format_details.tree_xid, 3)')->get(); 
+1

也可以實現這個使用'Model :: orderByRaw('SUBSTR(x_fs_format_details.tree_xid,3)') - > get( );'。如果在結果中不需要這些額外的數據,則更清潔。 – 2015-01-09 22:17:38

+0

@MarcoF。當然!我怎麼會想念那個。謝謝 – lukasgeiter 2015-01-10 07:38:19