【问题标题】:lumen order by with two level relation具有两级关系的流明顺序
【发布时间】:2018-11-18 09:56:32
【问题描述】:

这是一个任务管理器网络应用程序

我有 3 张桌子 rowstasksinputs

inputs 表有 order 列来选择首先显示哪一列

row 有很多tasks

input 有很多tasks

所以每个task 都有一个input 和一个row

我想按row_idinput.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();

它为每一列生成一行,而不是它假设的行

结果

[
    {
        "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": []
    }
]

这个文件是here

完整代码是here

【问题讨论】:

    标签: mysql laravel lumen


    【解决方案1】:

    因为它搜索表 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(); 
    

    【讨论】:

    • SQLSTATE[42S22]:找不到列:1054 'order 子句'中的未知列 'input.order'
    • 您在哪个表中有order 列?
    • inputs.order
    • 它忽略了顺序,和第三次尝试有什么不同?
    • 您是否以$result->tasks 的身份在$result = App\Row:: 进行检查?或$result->tasks->input ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多