错误:使用此函数时必须指定orderBy子句

时间:2019-02-20 08:42:48

标签: laravel laravel-5.4

尝试迁移时出现以下错误。实际上,我有很多数据,所以我使用了laravel中的块。

我正在使用MongoDB而不是SQL

server.use-forward-headers

我的迁移文件的启动功能是

true

我在这里经历了很多答案,几乎所有人都建议使用orderBy,所以我使用了它。但是仍然出现错误。

我用过

 Excel.Application Appl = null;
            Workbook opennwb = null;
            Worksheet wss = null;
            Workbooks opennwbs = null;
            // Save the whole row
            try
            {
                Appl = new Excel.Application();

                opennwbs = Appl.Workbooks;

                opennwb = opennwbs.Open(path,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                   Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                wss = opennwb.Worksheets[DateTime.Now.ToString("MM-dd-yy").ToString()];

                // Get the count of row and column
                int rows = wss.UsedRange.Rows.Count;
                int columns = 1;
                // Traverse all data
                for (int row = 1; row <= rows; row++)
                {
                    for (int column = 1; column <= columns; column++)
                    {
                        string temp = ((Range)wss.Cells[row, column]).Text.ToString();
                        // If true, get the correspond row
                        if (barcode == temp)
                        {


                            usedrow = row;



                        }

                    }

                }

                return usedrow;
            }
            finally
            {
                //MessageBox.Show("about to quit query excel");


                //release all memory - stop EXCEL.exe from hanging around.

                if (opennwb != null) { opennwb.Close(); Marshal.FinalReleaseComObject(opennwb); opennwb = null; } //release each workbook like this
                if (wss != null) { Marshal.FinalReleaseComObject(wss); wss = null; } //release each worksheet like this
                if (opennwbs != null) { opennwbs.Close(); Marshal.FinalReleaseComObject(opennwbs); opennwbs = null; }
                if (Appl != null) { Appl.Quit(); Marshal.FinalReleaseComObject(Appl); Appl = null; } //release the Excel application 

                GC.Collect();

                //MessageBox.Show("Query closed");

感谢您的帮助。预先感谢。

1 个答案:

答案 0 :(得分:3)

当Laravel内部使用enforceOrderBy内定义的Illuminate/Database/Query/Builder时,会添加此警告。

每当您在直接由DB门面Illuminate\Database\Query\Builder实例化的查询构建器上使用块时,它都会询问您:

  

使用此函数时,必须指定orderBy子句。

因此,如果您这样做,它将发生:

\DB::table('product')->chunk(10, function($product){
    ...
});

如果您手动向此订单添加订单,它将不会抛出错误并可以按预期运行:

\DB::table('product')->orderBy('created_at')->chunk(10, function($product){
    ...
});

但是,最好像直接使用Eloquent模型一样,这样不会强制您手动添加order by子句:

Product::chunk(10, function($product){
    ...
});

也没有方法DB::collection(),如果需要,可以改用DB::table(),除非您使用的是mongodb而不是MySQL