search

Home  >  Q&A  >  body text

laravel - laravle Eloquent ORM updates multiple records at one time

Eloquent batch update multiple records

Instead of batch assigning values ​​to multiple fields in one record,
but making different modifications to different records according to different conditions.

Similar to batch insert:

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

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

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

));

Is there any similar statement

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

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

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

) , 'id' );

The functions implemented are:
Modify the corresponding record according to the id:

id=1 'email' is changed to 'taylor1@example.com', 'votes' is changed to 1,
id=2 'email' is changed to 'dayle2@example.com', 'votes' is changed to 2
. . .

CI has a similar update_batch method. I want to convert it to laravel. Please give me more advice.

phpcn_u1582phpcn_u15822753 days ago573

reply all(1)I'll reply

  • 为情所困

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

    There is currently no good way to encapsulate the framework, but I just googled it and saw an answer that matches your question very well on stackoverflow. The following is copied from stackoverflow, the original link is 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')

    reply
    0
  • Cancelreply