pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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 class="g0yiu08" 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>

    1. <tfoot id='i0AmY'></tfoot>

        Eloquent 模型大規(guī)模更新

        Eloquent model mass update(Eloquent 模型大規(guī)模更新)
          <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 class="kay4024" 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>
                  <tbody id='Mk57M'></tbody>

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

                  <tfoot id='Mk57M'></tfoot>
                • 本文介紹了Eloquent 模型大規(guī)模更新的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  如果我錯了,請糾正我,但我認為 Eloquent 模型中沒有大規(guī)模更新這樣的東西.

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

                  有沒有辦法在不為每一行發(fā)出查詢的情況下對數據庫表進行批量更新?

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

                  比如有沒有靜態(tài)方法,比如

                  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...)

                  為什么沒有實現這樣的功能?如果發(fā)生這樣的事情,我是唯一一個會很高興的人嗎?

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

                  我(開發(fā)人員)不想像這樣實現它:

                  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!

                  有沒有這樣的靜態(tài)方法來執(zhí)行這個操作?如果沒有,我們是否可以擴展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?

                  推薦答案

                  對于大規(guī)模更新/插入功能,已被請求但 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() 方法動態(tài)獲取它.唯一的限制是您需要先初始化用戶類,然后才能使用此功能.您的查詢如下:

                  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

                  執(zhí)行此操作的其他方法(在您的情況下效率不高)是:

                  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();
                  }
                  

                  這樣表名就保存在用戶類中,你的開發(fā)人員不必擔心.

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

                  這篇關于Eloquent 模型大規(guī)模更新的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                        <tbody id='8UiSC'></tbody>

                      <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 class="u2maasy" id='8UiSC'><tfoot id='8UiSC'></tfoot><dl id='8UiSC'><fieldset id='8UiSC'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 辊道窑炉,辊道窑炉厂家-山东艾希尔| 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 电动葫芦|环链电动葫芦-北京凌鹰名优起重葫芦| 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 浙江工业冷却塔-菱电冷却塔厂家 - 浙江菱电冷却设备有限公司 | 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 天津中都白癜风医院_天津白癜风医院_天津治疗白癜风 | 广州小程序开发_APP开发公司_分销商城系统定制_小跑科技 | 27PR跨境电商导航 | 专注外贸跨境电商| 河北中仪伟创试验仪器有限公司是专业生产沥青,土工,水泥,混凝土等试验仪器的厂家,咨询电话:13373070969 | 天津云仓-天津仓储物流-天津云仓一件代发-顺东云仓 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 | 模型公司_模型制作_沙盘模型报价-中国模型网 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 档案密集架,移动密集架,手摇式密集架,吉林档案密集架-厂家直销★价格公道★质量保证 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 活性炭-蜂窝-椰壳-柱状-粉状活性炭-河南唐达净水材料有限公司 | 净气型药品柜-试剂柜-无管道净气型通风柜-苏州毕恩思 | 政府回应:200块在义乌小巷能买到爱情吗?——揭秘打工族省钱约会的生存智慧 | 河南空气能热水器-洛阳空气能采暖-洛阳太阳能热水工程-洛阳润达高科空气能商行 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 北京律师咨询_知名专业北京律师事务所_免费法律咨询 | 原色会计-合肥注册公司_合肥代理记账公司_营业执照代办 | 【连江县榕彩涂料有限公司】官方网站 | 微量水分测定仪_厂家_卡尔费休微量水分测定仪-淄博库仑 | 派克防爆伺服电机品牌|国产防爆伺服电机|高低温伺服电机|杭州摩森机电科技有限公司 | 台湾HIWIN上银直线模组|导轨滑块|TBI滚珠丝杆丝杠-深圳汉工 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 布袋式除尘器|木工除尘器|螺旋输送机|斗式提升机|刮板输送机|除尘器配件-泊头市德佳环保设备 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 车牌识别道闸_停车场收费系统_人脸识别考勤机_速通门闸机_充电桩厂家_中全清茂官网 | 考勤系统_考勤管理系统_网络考勤软件_政企|集团|工厂复杂考勤工时统计排班管理系统_天时考勤 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 活性炭厂家-蜂窝活性炭-粉状/柱状/果壳/椰壳活性炭-大千净化-活性炭 | 运动木地板厂家,篮球场木地板品牌,体育场馆木地板安装 - 欧氏运动地板 | 锂电池砂磨机|石墨烯砂磨机|碳纳米管砂磨机-常州市奥能达机械设备有限公司 | 混合生育酚_醋酸生育酚粉_琥珀酸生育酚-山东新元素生物科技 | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 |