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

      <legend id='Mk57M'><style id='Mk57M'><dir id='Mk57M'><q id='Mk57M'></q></dir></style></legend>
          <bdo id='Mk57M'></bdo><ul id='Mk57M'></ul>

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

        <tfoot id='Mk57M'></tfoot>
      1. Eloquent 模型大规模更新

        时间:2023-09-24
      2. <i id='i0AmY'><tr id='i0AmY'><dt id='i0AmY'><q id='i0AmY'><span id='i0AmY'><b id='i0AmY'><form id='i0AmY'><ins id='i0AmY'></ins><ul id='i0AmY'></ul><sub id='i0AmY'></sub></form><legend id='i0AmY'></legend><bdo id='i0AmY'><pre id='i0AmY'><center id='i0AmY'></center></pre></bdo></b><th id='i0AmY'></th></span></q></dt></tr></i><div id='i0AmY'><tfoot id='i0AmY'></tfoot><dl id='i0AmY'><fieldset id='i0AmY'></fieldset></dl></div>
            <bdo id='i0AmY'></bdo><ul id='i0AmY'></ul>

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

                <legend id='i0AmY'><style id='i0AmY'><dir id='i0AmY'><q id='i0AmY'></q></dir></style></legend>

                  <tbody id='i0AmY'></tbody>
                <tfoot id='i0AmY'></tfoot>

                  本文介绍了Eloquent 模型大规模更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果我错了,请纠正我,但我认为 Eloquent 模型中没有大规模更新这样的东西.

                  Please correct me if I am wrong, but I think there is no such thing as mass update in an Eloquent model.

                  有没有办法在不为每一行发出查询的情况下对数据库表进行批量更新?

                  Is there a way to make a mass update on the DB table without issuing a query for every row?

                  比如有没有静态方法,比如

                  For example, is there a static method, something like

                  User::updateWhere(
                      array('age', '<', '18'),
                      array(
                          'under_18' => 1 
                          [, ...]
                      )
                  );
                  

                  (是的,这是一个愚蠢的例子,但你得到了图片......)

                  (yes, it is a silly example but you get the picture...)

                  为什么没有实现这样的功能?如果发生这样的事情,我是唯一一个会很高兴的人吗?

                  Why isn't there such a feature implemented? Am I the only one who would be very happy if something like this comes up?

                  我(开发人员)不想像这样实现它:

                  I (the developers), wouldn't like to implement it like:

                  DB::table('users')->where('age', '<', '18')->update(array('under_18' => 1));
                  

                  因为随着项目的增长,我们以后可能会要求程序员更改表名,而他们无法搜索和替换表名!

                  because as the project grows, we may require the programmers to change the table name in the future and they cannot search and replace for the table name!

                  有没有这样的静态方法来执行这个操作?如果没有,我们是否可以扩展IlluminateDatabaseEloquentModel 类来完成这样的事情?

                  Is there such a static method to perform this operation? And if there is not, can we extend the IlluminateDatabaseEloquentModel class to accomplish such a thing?

                  推荐答案

                  对于大规模更新/插入功能,已被请求但 Taylor Otwell(Laravel 作者)建议用户应改用 Query Builder.https://github.com/laravel/framework/issues/1295

                  For mass update/insert features, it was requested but Taylor Otwell (Laravel author) suggest that users should use Query Builder instead. https://github.com/laravel/framework/issues/1295

                  你的模型通常应该扩展 IlluminateDatabaseEloquentModel.然后你访问实体本身,例如,如果你有这个:

                  Your models should generally extend IlluminateDatabaseEloquentModel. Then you access the entity iself, for example if you have this:

                  <?php
                  Use IlluminateDatabaseEloquentModel;
                  
                  class User extends Model {
                  
                      // table name defaults to "users" anyway, so this definition is only for
                      // demonstration on how you can set a custom one
                      protected $table = 'users';
                      // ... code omited ...
                  

                  更新 #2

                  您必须求助于查询构建器.为了解决表命名问题,您可以通过 getTable() 方法动态获取它.唯一的限制是您需要先初始化用户类,然后才能使用此功能.您的查询如下:

                  You have to resort to query builder. To cover table naming issue, you could get it dynamically via getTable() method. The only limitation of this is that you need your user class initialized before you can use this function. Your query would be as follows:

                  $userTable = (new User())->getTable();
                  DB::table($userTable)->where('age', '<', 18)->update(array('under_18' => 1));
                  

                  这样你的表名就是用户模型中的控制器(如上例所示).

                  This way your table name is controller in User model (as shown in the example above).

                  更新 #1

                  执行此操作的其他方法(在您的情况下效率不高)是:

                  Other way to do this (not efficient in your situation) would be:

                  $users = User::where('age', '<', 18)->get();
                  foreach ($users as $user) {
                      $user->field = value;
                      $user->save();
                  }
                  

                  这样表名就保存在用户类中,你的开发人员不必担心.

                  This way the table name is kept in users class and your developers don't have to worry about it.

                  这篇关于Eloquent 模型大规模更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何合并两个 Eloquent 集合? 下一篇:Eloquent 多对多对多 - 如何轻松加载远距离关系

                  相关文章

                  最新文章

                  1. <legend id='8UiSC'><style id='8UiSC'><dir id='8UiSC'><q id='8UiSC'></q></dir></style></legend><tfoot id='8UiSC'></tfoot>

                    <small id='8UiSC'></small><noframes id='8UiSC'>

                      <bdo id='8UiSC'></bdo><ul id='8UiSC'></ul>

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