порядок люмен с двухуровневым отношением - PullRequest
0 голосов
/ 08 июня 2018

это веб-приложение диспетчера задач

у меня есть 3 таблицы rows, tasks и inputs

в таблице inputs есть столбец order, на выбор которогостолбец, который будет отображаться первым

row имеет много tasks

input имеет много tasks

, поэтому каждый task имеет input иrow

Я хочу отсортировать задачи по row_id, а затем по input.order

сначала попытаться

App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'])
  ->orderBy('rows.id', 'ASC', 'order', 'ASC')
  ->get(); 

игнорирует порядок

результат

[
    {
        "id": 1,
        "user_id": 1,
        "created_at": "2018-06-07 18:40:23",
        "updated_at": "2018-06-07 18:40:23",
        "tasks": [
            {
                "id": 1,
                "row_id": 1,
                "input_id": 1,
                "option_id": 1,
                "value": null,
                "input": {
                    "id": 1,
                    "name": "assigned to",
                    "type": 1,
                    "value": "mario",
                    "required": 1,
                    "order": 2,
                    "user_id": 1,
                    "created_at": "2018-06-07 18:40:16",
                    "updated_at": "2018-06-08 10:08:19"
                }
            },
            {
                "id": 2,
                "row_id": 1,
                "input_id": 2,
                "option_id": null,
                "value": "test new option",
                "input": {
                    "id": 2,
                    "name": "test new option",
                    "type": 0,
                    "value": "test new option",
                    "required": 0,
                    "order": 3,
                    "user_id": 1,
                    "created_at": "2018-06-07 18:40:44",
                    "updated_at": "2018-06-08 10:08:19"
                }
            },
            {
                "id": 3,
                "row_id": 1,
                "input_id": 3,
                "option_id": null,
                "value": "2018-07-01",
                "input": {
                    "id": 3,
                    "name": "deadline",
                    "type": 3,
                    "value": "2018-07-01",
                    "required": 0,
                    "order": 4,
                    "user_id": 1,
                    "created_at": "2018-06-08 10:07:37",
                    "updated_at": "2018-06-08 10:08:19"
                }
            },
            {
                "id": 4,
                "row_id": 1,
                "input_id": 4,
                "option_id": null,
                "value": "",
                "input": {
                    "id": 4,
                    "name": "priority",
                    "type": 6,
                    "value": "",
                    "required": 0,
                    "order": 1,
                    "user_id": 1,
                    "created_at": "2018-06-08 10:08:19",
                    "updated_at": "2018-06-08 10:08:19"
                }
            }
        ]
    }
]

вторая попытка

App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'])
  ->orderBy('rows.id', 'ASC')
  ->orderBy('order', 'ASC')
  ->get(); 

но сгенерирована ошибка

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order' in 'order clause' (SQL: select * from `rows` order by `rows`.`id` asc, `order` asc)

третья попытка

App\Row::with([
  'user', 
  'tasks', 
  'tasks.option', 
  'tasks.input' => 
    function($query) {
        $query->orderBy('order', 'ASC');
    }
  ])
  ->orderBy('rows.id', 'ASC')
  ->get();

этоигнорирует результат порядка, аналогичный первому

далее, попробуйте

    return App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'])
        ->join('tasks', 'rows.id', '=', 'tasks.row_id')
        ->join('inputs', 'inputs.id', '=', 'tasks.input_id')
        ->orderBy('rows.id', 'ASC')
        ->orderBy( 'inputs.order', 'ASC')
        ->get();

, он генерирует строку для каждого столбца, а не строку, как это предполагается для

result

[
    {
        "id": 4,
        "user_id": 1,
        "created_at": "2018-06-08 10:08:19",
        "updated_at": "2018-06-08 10:08:19",
        "row_id": 1,
        "input_id": 4,
        "option_id": null,
        "value": "",
        "name": "priority",
        "type": 6,
        "required": 0,
        "order": 1,
        "tasks": []
    },
    {
        "id": 1,
        "user_id": 1,
        "created_at": "2018-06-07 18:40:16",
        "updated_at": "2018-06-08 10:08:19",
        "row_id": 1,
        "input_id": 1,
        "option_id": 1,
        "value": "mario",
        "name": "assigned to",
        "type": 1,
        "required": 1,
        "order": 2,
        "tasks": [
            {
                "id": 1,
                "row_id": 1,
                "input_id": 1,
                "option_id": 1,
                "value": null,
                "input": {
                    "id": 1,
                    "name": "assigned to",
                    "type": 1,
                    "value": "mario",
                    "required": 1,
                    "order": 2,
                    "user_id": 1,
                    "created_at": "2018-06-07 18:40:16",
                    "updated_at": "2018-06-08 10:08:19"
                }
            },
            {
                "id": 2,
                "row_id": 1,
                "input_id": 2,
                "option_id": null,
                "value": "test new option",
                "input": {
                    "id": 2,
                    "name": "test new option",
                    "type": 0,
                    "value": "test new option",
                    "required": 0,
                    "order": 3,
                    "user_id": 1,
                    "created_at": "2018-06-07 18:40:44",
                    "updated_at": "2018-06-08 10:08:19"
                }
            },
            {
                "id": 3,
                "row_id": 1,
                "input_id": 3,
                "option_id": null,
                "value": "2018-07-01",
                "input": {
                    "id": 3,
                    "name": "deadline",
                    "type": 3,
                    "value": "2018-07-01",
                    "required": 0,
                    "order": 4,
                    "user_id": 1,
                    "created_at": "2018-06-08 10:07:37",
                    "updated_at": "2018-06-08 10:08:19"
                }
            },
            {
                "id": 4,
                "row_id": 1,
                "input_id": 4,
                "option_id": null,
                "value": "",
                "input": {
                    "id": 4,
                    "name": "priority",
                    "type": 6,
                    "value": "",
                    "required": 0,
                    "order": 1,
                    "user_id": 1,
                    "created_at": "2018-06-08 10:08:19",
                    "updated_at": "2018-06-08 10:08:19"
                }
            }
        ]
    },
    {
        "id": 2,
        "user_id": 1,
        "created_at": "2018-06-07 18:40:44",
        "updated_at": "2018-06-08 10:08:19",
        "row_id": 1,
        "input_id": 2,
        "option_id": null,
        "value": "test new option",
        "name": "test new option",
        "type": 0,
        "required": 0,
        "order": 3,
        "tasks": []
    },
    {
        "id": 3,
        "user_id": 1,
        "created_at": "2018-06-08 10:07:37",
        "updated_at": "2018-06-08 10:08:19",
        "row_id": 1,
        "input_id": 3,
        "option_id": null,
        "value": "2018-07-01",
        "name": "deadline",
        "type": 3,
        "required": 0,
        "order": 4,
        "tasks": []
    }
]

этот файл здесь

полный код здесь

1 Ответ

0 голосов
/ 08 июня 2018

Поскольку он ищет столбцы в таблице rows, а не в input.

  App\Row::with(['user', 'tasks', 'tasks.option', 'tasks.input'=> function ($q){
        $q->orderBy('order', 'ASC'); // order from "rows" table
    }])
  ->orderBy('id', 'ASC') // id from "rows" table
  ->get(); 
...