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

    3. <tfoot id='4eWjs'></tfoot>

        如何使用 Laravel 4.1 基于 id 或对象数组执行批量删

        时间:2023-09-23
          • <bdo id='iU8TY'></bdo><ul id='iU8TY'></ul>
                <tbody id='iU8TY'></tbody>

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

                1. <tfoot id='iU8TY'></tfoot>
                2. <legend id='iU8TY'><style id='iU8TY'><dir id='iU8TY'><q id='iU8TY'></q></dir></style></legend>

                  本文介绍了如何使用 Laravel 4.1 基于 id 或对象数组执行批量删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我只是想知道是否有可能.

                  I just wanted to know if it's possible.

                  我知道当您要插入多行时,您可以构建一个数组并执行以下操作:

                  I know when you have multiple rows to insert, you can just build an array and do something like:

                  DB::table('some_table')->insert($array);
                  

                  但据我所知,对删除做同样的事情似乎是不可能的,我想知道是否有人知道这样做的方法:

                  But as far as I've read, doing the same for deleting doesn't seem to be possible, I'd like to know if anyone know of a way to do something like:

                  DB::table('some_table')->delete($array);
                  

                  推荐答案

                  Laravel 4.1 中删除记录的多种方式

                  1) 当您想从数据库中删除记录时,只需调用 delete 方法:

                  1) When you want to delete records from your database, simply call the delete method:

                  $affected = DB::table('users')->where('id', '=', 1)->delete();
                  

                  2) 想通过 ID 快速删除记录?没问题.只需将 ID 传递到删除方法中即可:

                  2) Want to quickly delete a record by its ID? No problem. Just pass the ID into the delete method:

                  $affected = DB::table('users')->delete(1);
                  

                  3) 如果你想一次删除多个记录的 id,将它们的 id 传递到一个数组中 - 使用以下内容

                  $users_to_delete = array(1, 2, 3);
                  DB::table('users')->whereIn('id', $users_to_delete)->delete(); 
                  

                  4) 如果你想一次通过id删除多条记录,传递一个用户数组——使用下面的

                          //(case A) User fields indexed by number 0,1,2..
                          $users_to_delete = array(
                             '0'=> array('1','Frank','Smith','Whatever'), 
                             '1'=> array('5','John','Johnson','Whateverelse'),
                          );
                  
                          $ids_to_delete = array_map(function($item){ return $item[0]; }, $users_to_delete);
                  
                          DB::table('users')->whereIn('id', $ids_to_delete)->delete(); 
                  
                          //(case B) User fields indexed by key
                          $users_to_delete = array(
                             '0'=> array('id'=>'1','name'=>'Frank','surname'=>'Smith','title'=>'Whatever'), 
                             '1'=> array('id'=>'5','name'=>'John','surname'=>'Johnson','title'=>'Whateverelse'),
                          );
                  
                          $ids_to_delete = array_map(function($item){ return $item['id']; }, $users_to_delete);
                  
                          DB::table('users')->whereIn('id', $ids_to_delete)->delete(); 
                  

                  5) 按键删除现有模型

                  5) Deleting An Existing Model By Key

                  User::destroy(1);
                  User::destroy(array(1, 2, 3));
                  User::destroy(1, 2, 3);
                  

                  6) 当然,您也可以对一组模型运行删除查询:

                  6) Of course, you may also run a delete query on a set of models:

                  $affectedRows = User::where('votes', '>', 100)->delete();
                  

                  这篇关于如何使用 Laravel 4.1 基于 id 或对象数组执行批量删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 如何在 Eloquent 模型中添加自定义函数? 下一篇:laravel 搜索多个以空格分隔的单词

                  相关文章

                  最新文章

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

                    3. <tfoot id='VDroI'></tfoot>

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