Illuminate\Database\Query\Builder::insert PHP Method

insert() public method

Insert a new record into the database.
public insert ( array $values ) : boolean
$values array
return boolean
    public function insert(array $values)
    {
        if (empty($values)) {
            return true;
        }
        // Since every insert gets treated like a batch insert, we will make sure the
        // bindings are structured in a way that is convenient for building these
        // inserts statements by verifying the elements are actually an array.
        if (!is_array(reset($values))) {
            $values = [$values];
        } else {
            foreach ($values as $key => $value) {
                ksort($value);
                $values[$key] = $value;
            }
        }
        // We'll treat every insert like a batch insert so we can easily insert each
        // of the records into the database consistently. This will make it much
        // easier on the grammars to just handle one type of record insertion.
        $bindings = [];
        foreach ($values as $record) {
            foreach ($record as $value) {
                $bindings[] = $value;
            }
        }
        $sql = $this->grammar->compileInsert($this, $values);
        // Once we have compiled the insert statement's SQL we can execute it on the
        // connection and return a result as a boolean success indicator as that
        // is the same type of result returned by the raw connection instance.
        $bindings = $this->cleanBindings($bindings);
        return $this->connection->insert($sql, $bindings);
    }

Usage Example

コード例 #1
0
 /**
  * Construct Closet object with owner's uid.
  *
  * @param int $uid
  */
 public function __construct($uid)
 {
     $this->uid = $uid;
     $this->db = DB::table('closets');
     // create a new closet if not exists
     if (!$this->db->where('uid', $uid)->get()) {
         $this->db->insert(['uid' => $uid, 'textures' => '']);
     }
     // load items from json string
     $this->textures = json_decode($this->db->where('uid', $uid)->get()[0]->textures, true);
     $this->textures = is_array($this->textures) ? $this->textures : [];
     $textures_invalid = [];
     // traverse items in the closet
     foreach ($this->textures as $texture) {
         $result = Texture::find($texture['tid']);
         if ($result) {
             // set user custom texture name
             $result->name = $texture['name'];
             // push instances of App\Models\Texture to the bag
             if ($result->type == "cape") {
                 $this->textures_cape[] = $result;
             } else {
                 $this->textures_skin[] = $result;
             }
         } else {
             $textures_invalid[] = $texture['tid'];
             continue;
         }
     }
     // remove invalid textures from closet
     foreach ($textures_invalid as $tid) {
         $this->remove($tid);
     }
     unset($textures_invalid);
 }
All Usage Examples Of Illuminate\Database\Query\Builder::insert