搜索

首页  >  问答  >  正文

laravel - laravle Eloquent ORM 一次update多条记录

Eloquent 批量更新多条记录

不是对一条记录多个字段批量赋值,
而是根据不同条件对不同记录做不同修改

类似 批量插入:

DB::table('users')->insert(array(

array('email' => 'taylor@example.com', 'votes' => 0),

array('email' => 'dayle@example.com', 'votes' => 0),

));

有没有类似的语句

DB::table('users')->update( array(

array('id'=>1, 'email' => 'taylor1@example.com', 'votes' => 1),

array('id'=>2, 'email' => 'dayle2@example.com', 'votes' => 2),

) , 'id' );

实现的功能是:
根据id修改相应的记录:

id=1 'email' 修改成'taylor1@example.com', 'votes' 修改成 1,
id=2 'email' 修改成'dayle2@example.com', 'votes' 修改为 2
。。。

ci是有类似的update_batch方法,想转换成laravel,请多指点了。

phpcn_u1582phpcn_u15822790 天前602

全部回复(1)我来回复

  • 为情所困

    为情所困2017-05-16 16:56:58

    框架封装好的方法目前是没有的,但是随手google了一下,在stackoverflow上看到一个和你的这个问题非常匹配的回答,以下复制于stackoverflow,原链接http://stackoverflow.com/questions/26133977/laravel-bulk-update。
    I have created My Custom function for Multiple Update like update_batch in CodeIgniter.
    Just place this function in any of your model or you can create helper class and place this function in that class:

    //test data
    /*
    $multipleData = array(
       array(
          'title' => 'My title' ,
          'name' => 'My Name 2' ,
          'date' => 'My date 2'
       ),
       array(
          'title' => 'Another title' ,
          'name' => 'Another Name 2' ,
          'date' => 'Another date 2'
       )
    )
    */
    
    /*
     * ----------------------------------
     * update batch 
     * ----------------------------------
     * 
     * multiple update in one query
     *
     * tablename( required | string )
     * multipleData ( required | array of array )
     */
    static function updateBatch($tableName = "", $multipleData = array()){
    
        if( $tableName && !empty($multipleData) ) {
    
            // column or fields to update
            $updateColumn = array_keys($multipleData[0]);
            $referenceColumn = $updateColumn[0]; //e.g id
            unset($updateColumn[0]);
            $whereIn = "";
    
            $q = "UPDATE ".$tableName." SET "; 
            foreach ( $updateColumn as $uColumn ) {
                $q .=  $uColumn." = CASE ";
    
                foreach( $multipleData as $data ) {
                    $q .= "WHEN ".$referenceColumn." = ".$data[$referenceColumn]." THEN '".$data[$uColumn]."' ";
                }
                $q .= "ELSE ".$uColumn." END, ";
            }
            foreach( $multipleData as $data ) {
                $whereIn .= "'".$data[$referenceColumn]."', ";
            }
            $q = rtrim($q, ", ")." WHERE ".$referenceColumn." IN (".  rtrim($whereIn, ', ').")";
    
            // Update  
            return DB::update(DB::raw($q));
    
        } else {
            return false;
        }
    }

    It will Produces:

    UPDATE `mytable` SET `name` = CASE
    WHEN `title` = 'My title' THEN 'My Name 2'
    WHEN `title` = 'Another title' THEN 'Another Name 2'
    ELSE `name` END,
    `date` = CASE 
    WHEN `title` = 'My title' THEN 'My date 2'
    WHEN `title` = 'Another title' THEN 'Another date 2'
    ELSE `date` END
    WHERE `title` IN ('My title','Another title')

    回复
    0
  • 取消回复