搜尋

首頁  >  問答  >  主體

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_u15822753 天前574

全部回覆(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 inthis 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
  • 取消回覆