/** * Create a new session driver instance. * * @param string $driver * @return Driver */ public static function make($driver) { switch ($driver) { case 'apc': return new APC(Cache::driver('apc')); case 'cookie': return new Cookie(); case 'database': return new Database(\Laravel\Database\Manager::connection()); case 'file': return new File(SESSION_PATH); case 'memcached': return new Memcached(Cache::driver('memcached')); case 'redis': return new Redis(Cache::driver('redis')); default: throw new \DomainException("Session driver [{$driver}] is not supported."); } }
/** * Adjust the value of a column up or down by a given amount. * * @param string $column * @param int $amount * @param string $operator * @return int */ protected function adjust($column, $amount, $operator) { $value = Manager::raw($this->grammar->wrap($column) . $operator . $amount); return $this->update(array($column => $value)); }
/** * Delete a model from the database. * * @param int $id * @return int */ public function delete($id = null) { // If the delete method is being called on an existing model, we only want to delete // that model. If it is being called from an Eloquent query model, it is probably // the developer's intention to delete more than one model, so we will pass the // delete statement to the query instance. if (!$this->exists) { return $this->query->delete(); } $table = static::table(get_class($this)); return DB::connection(static::$connection)->table($table)->delete($this->id); }
/** * Execute a SQL query against the connection. * * The method returns the following based on query type: * * SELECT -> Array of stdClasses * UPDATE -> Number of rows affected. * DELETE -> Number of Rows affected. * ELSE -> Boolean true / false depending on success. * * <code> * // Execute a query against the database connection * $users = DB::connection()->query('select * from users'); * * // Execute a query with bound parameters * $user = DB::connection()->query('select * from users where id = ?', array($id)); * </code> * * @param string $sql * @param array $bindings * @return mixed */ public function query($sql, $bindings = array()) { // Since expressions are injected into the query as raw strings, we need // to remove them from the array of bindings. They are not truly bound // to the PDO statement as named parameters. foreach ($bindings as $key => $value) { if ($value instanceof Expression) { unset($bindings[$key]); } } $bindings = array_values($bindings); $sql = $this->transform($sql, $bindings); $this->queries[] = compact('sql', 'bindings'); try { $prepared_statement = @$this->pdo->prepare($sql); $result = $this->execute($prepared_statement, $bindings); return $result; } catch (\PDOException $e) { if (strstr($e->getMessage(), 'MySQL server has gone away') !== false && strpos(strtoupper($sql), 'SELECT') === 0 && !$this->pdo->inTransaction()) { $this->pdo = Manager::connect($this->config); $prepared_statement = @$this->pdo->prepare($sql); $result = $this->execute($prepared_statement, $bindings); return $result; } else { throw $e; } } }
/** * Validate the uniqueness of an attribute value on a given database table. * * If a database column is not specified, the attribute name will be used. * * @param string $attribute * @param mixed $value * @param array $parameters * @return bool */ protected function validate_unique($attribute, $value, $parameters) { if (!isset($parameters[1])) { $parameters[1] = $attribute; } if (is_null($this->db)) { $this->db = DB::connection(); } return $this->db->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0; }
/** * Delete a model from the database. * * @param int $id * @return int */ public function delete($id = null) { // If the delete method is being called on an existing model, we only want to delete // that model. If it is being called from an Eloquent query model, it is probably // the developer's intention to delete more than one model, so we will pass the // delete statement to the query instance. if (!$this->exists) { return $this->query->delete(); } $success = DB::connection(static::$connection)->table(static::table(get_class($this)))->delete($this->id); if ($success) { \Events::launch(EVENT_ENTITY_DELETED, ['id' => $this->id, 'class' => get_class($this)]); } return $success; }