<bdo id='QHn8f'></bdo><ul id='QHn8f'></ul>
  1. <legend id='QHn8f'><style id='QHn8f'><dir id='QHn8f'><q id='QHn8f'></q></dir></style></legend>

  2. <i id='QHn8f'><tr id='QHn8f'><dt id='QHn8f'><q id='QHn8f'><span id='QHn8f'><b id='QHn8f'><form id='QHn8f'><ins id='QHn8f'></ins><ul id='QHn8f'></ul><sub id='QHn8f'></sub></form><legend id='QHn8f'></legend><bdo id='QHn8f'><pre id='QHn8f'><center id='QHn8f'></center></pre></bdo></b><th id='QHn8f'></th></span></q></dt></tr></i><div id='QHn8f'><tfoot id='QHn8f'></tfoot><dl id='QHn8f'><fieldset id='QHn8f'></fieldset></dl></div>
  3. <tfoot id='QHn8f'></tfoot>
  4. <small id='QHn8f'></small><noframes id='QHn8f'>

      Laravel - 多插入行和检索 id

      时间:2023-10-31
      <legend id='DTkpC'><style id='DTkpC'><dir id='DTkpC'><q id='DTkpC'></q></dir></style></legend>
        <bdo id='DTkpC'></bdo><ul id='DTkpC'></ul>

        <small id='DTkpC'></small><noframes id='DTkpC'>

        • <tfoot id='DTkpC'></tfoot>

            <i id='DTkpC'><tr id='DTkpC'><dt id='DTkpC'><q id='DTkpC'><span id='DTkpC'><b id='DTkpC'><form id='DTkpC'><ins id='DTkpC'></ins><ul id='DTkpC'></ul><sub id='DTkpC'></sub></form><legend id='DTkpC'></legend><bdo id='DTkpC'><pre id='DTkpC'><center id='DTkpC'></center></pre></bdo></b><th id='DTkpC'></th></span></q></dt></tr></i><div id='DTkpC'><tfoot id='DTkpC'></tfoot><dl id='DTkpC'><fieldset id='DTkpC'></fieldset></dl></div>

                  <tbody id='DTkpC'></tbody>
                本文介绍了Laravel - 多插入行和检索 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我使用的是 Laravel 4,我需要在 MySQL 表中插入一些行,并且我需要取回它们插入的 ID.

                I'm using Laravel 4, and I need to insert some rows into a MySQL table, and I need to get their inserted IDs back.

                对于单行,我可以使用 ->insertGetId(),但是它不支持多行.如果我至少可以像普通 MySQL 那样检索第一行的 ID,就足以找出其他行了.

                For a single row, I can use ->insertGetId(), however it has no support for multiple rows. If I could at least retrieve the ID of the first row, as plain MySQL does, would be enough to figure out the other ones.

                推荐答案

                正如用户 Xrymz 建议的那样,DB::raw('LAST_INSERT_ID();') 返回第一个.

                As user Xrymz suggested, DB::raw('LAST_INSERT_ID();') returns the first.

                根据Schema api insertGetId() 接受数组

                public int insertGetId(array $values, string $sequence = null)

                所以你必须能够做到

                DB::table('table')->insertGetId($arrayValues);

                也就是说,如果使用 MySQL,您可以通过此检索第一个 id 并计算其余部分.还有一个 DB::getPdo()->lastInsertId(); 函数,可以提供帮助.

                Thats speaking, if using MySQL, you could retrive the first id by this and calculate the rest. There is also a DB::getPdo()->lastInsertId(); function, that could help.

                或者,如果它使用某些方法返回了最后一个 id,您也可以将其计算回第一个插入的 id.

                Or if it returened the last id with some of this methods, you can calculate it back to the first inserted too.

                编辑

                根据评论,我的建议可能是错误的.

                According to comments, my suggestions may be wrong.

                关于'如果行被另一个用户插入中间'的问题,它 取决于 商店引擎.如果使用具有表级锁定的引擎(MyISAMMEMORYMERGE),那么这个问题就无关紧要了,因为不能同时有两个写入表.

                Regarding the question of 'what if row is inserted by another user inbetween', it depends on the store engine. If engine with table level locking (MyISAM, MEMORY, and MERGE) is used, then the question is irrevelant, since thete cannot be two simultaneous writes to the table.

                如果使用行级锁定引擎(InnoDB),那么,另一种可能是只插入数据,然后使用 whereIn() 方法,或者找出 表级锁定.

                If row-level locking engine is used (InnoDB), then, another possibility might be to just insert the data, and then retrieve all the rows by some known field with whereIn() method, or figure out the table level locking.

                这篇关于Laravel - 多插入行和检索 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:laravel 4持续数据正在进行 - jquery ajax提交 下一篇:Laravel db 迁移 - renameColumn 错误 - 请求了未知的数据库类型枚举

                相关文章

              • <small id='V3N3P'></small><noframes id='V3N3P'>

                  <bdo id='V3N3P'></bdo><ul id='V3N3P'></ul>

                  <i id='V3N3P'><tr id='V3N3P'><dt id='V3N3P'><q id='V3N3P'><span id='V3N3P'><b id='V3N3P'><form id='V3N3P'><ins id='V3N3P'></ins><ul id='V3N3P'></ul><sub id='V3N3P'></sub></form><legend id='V3N3P'></legend><bdo id='V3N3P'><pre id='V3N3P'><center id='V3N3P'></center></pre></bdo></b><th id='V3N3P'></th></span></q></dt></tr></i><div id='V3N3P'><tfoot id='V3N3P'></tfoot><dl id='V3N3P'><fieldset id='V3N3P'></fieldset></dl></div>
                  1. <legend id='V3N3P'><style id='V3N3P'><dir id='V3N3P'><q id='V3N3P'></q></dir></style></legend>

                    <tfoot id='V3N3P'></tfoot>