setRawAttributes($attributes, true); $this->setTable($table); $this->setConnection($parent->getConnectionName()); // We store off the parent instance so we will access the timestamp column names // for the model, since the pivot model timestamps aren't easily configurable // from the developer's point of view. We can use the parents to get these. $this->parent = $parent; $this->exists = $exists; $this->timestamps = $this->hasTimestampAttributes(); } /** * Set the keys for a save update query. * * @param \Illuminate\Database\Eloquent\Builder * @return \Illuminate\Database\Eloquent\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->foreignKey, $this->getAttribute($this->foreignKey)); return $query->where($this->otherKey, $this->getAttribute($this->otherKey)); } /** * Delete the pivot model record from the database. * * @return int */ public function delete() { return $this->getDeleteQuery()->delete(); } /** * Get the query builder for a delete operation on the pivot. * * @return \Illuminate\Database\Eloquent\Builder */ protected function getDeleteQuery() { $foreign = $this->getAttribute($this->foreignKey); $query = $this->newQuery()->where($this->foreignKey, $foreign); return $query->where($this->otherKey, $this->getAttribute($this->otherKey)); } /** * Get the foreign key column name. * * @return string */ public function getForeignKey() { return $this->foreignKey; } /** * Get the "other key" column name. * * @return string */ public function getOtherKey() { return $this->otherKey; } /** * Set the key names for the pivot model instance. * * @param string $foreignKey * @param string $otherKey * @return $this */ public function setPivotKeys($foreignKey, $otherKey) { $this->foreignKey = $foreignKey; $this->otherKey = $otherKey; return $this; } /** * Determine if the pivot model has timestamp attributes. * * @return bool */ public function hasTimestampAttributes() { return array_key_exists($this->getCreatedAtColumn(), $this->attributes); } /** * Get the name of the "created at" column. * * @return string */ public function getCreatedAtColumn() { return $this->parent->getCreatedAtColumn(); } /** * Get the name of the "updated at" column. * * @return string */ public function getUpdatedAtColumn() { return $this->parent->getUpdatedAtColumn(); } }