目的:私が今回やろうとしたい事は、DBの重複行をまとめた際の順序を変えたい事です。
削除ではなくまとめると言うことで、distinct()を使用しています。
DBは下記のような感じです
id | main |
---|---|
1 | test1 |
2 | test2 |
3 | test3 |
~ | 以下省略 |
まとめるトコロまでは↓のソースでできたのですが、
-TestController- $tests = DB::table('m_test')->select('main')->distinct()->get(); dd($tests);
ddで中を見てみると、↓の様に順序がバラバラだったのです。
Collection {#215 ▼ #items: array:6 [▼ 0 => {#205 ▼ +"main": "test3" } 1 => {#268 ▼ +"main": "test1" } 2 => {#269 ▼ +"main": "test2" } ~ 以下省略 ~ ] }
・解決方法として、orderByを使うのかなと思い、
$tests = DB::table('m_test')->select('main')->distinct()->orderBy('id', 'asc');
や、
$tests = DB::table('m_test')->select('main')->distinct()->orderBy('id', 'asc')->get();
$tests = DB::table('m_test')->orderBy('id', 'asc')->select('main')->distinct()->get();
$tests = DB::table('m_test')->select('main')->distinct()->orderBy('main', 'asc');
等、いろいろ試しましたが、エラーが出てしまいます。
どうすればよいか分からず困っているので、教えていただけたらと思います

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/02 00:05