123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
-
- namespace Illuminate\Database\Eloquent\Relations;
-
- use Illuminate\Support\Str;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Builder;
-
- class Pivot extends Model
- {
- /**
- * The parent model of the relationship.
- *
- * @var \Illuminate\Database\Eloquent\Model
- */
- public $pivotParent;
-
- /**
- * The name of the foreign key column.
- *
- * @var string
- */
- protected $foreignKey;
-
- /**
- * The name of the "other key" column.
- *
- * @var string
- */
- protected $relatedKey;
-
- /**
- * The attributes that aren't mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
-
- /**
- * Create a new pivot model instance.
- *
- * @param \Illuminate\Database\Eloquent\Model $parent
- * @param array $attributes
- * @param string $table
- * @param bool $exists
- * @return static
- */
- public static function fromAttributes(Model $parent, $attributes, $table, $exists = false)
- {
- $instance = new static;
-
- // The pivot model is a "dynamic" model since we will set the tables dynamically
- // for the instance. This allows it work for any intermediate tables for the
- // many to many relationship that are defined by this developer's classes.
- $instance->setConnection($parent->getConnectionName())
- ->setTable($table)
- ->forceFill($attributes)
- ->syncOriginal();
-
- // 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.
- $instance->pivotParent = $parent;
-
- $instance->exists = $exists;
-
- $instance->timestamps = $instance->hasTimestampAttributes();
-
- return $instance;
- }
-
- /**
- * Create a new pivot model from raw values returned from a query.
- *
- * @param \Illuminate\Database\Eloquent\Model $parent
- * @param array $attributes
- * @param string $table
- * @param bool $exists
- * @return static
- */
- public static function fromRawAttributes(Model $parent, $attributes, $table, $exists = false)
- {
- $instance = static::fromAttributes($parent, [], $table, $exists);
-
- $instance->setRawAttributes($attributes, true);
-
- $instance->timestamps = $instance->hasTimestampAttributes();
-
- return $instance;
- }
-
- /**
- * Set the keys for a save update query.
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- protected function setKeysForSaveQuery(Builder $query)
- {
- if (isset($this->attributes[$this->getKeyName()])) {
- return parent::setKeysForSaveQuery($query);
- }
-
- $query->where($this->foreignKey, $this->getOriginal(
- $this->foreignKey, $this->getAttribute($this->foreignKey)
- ));
-
- return $query->where($this->relatedKey, $this->getOriginal(
- $this->relatedKey, $this->getAttribute($this->relatedKey)
- ));
- }
-
- /**
- * Delete the pivot model record from the database.
- *
- * @return int
- */
- public function delete()
- {
- if (isset($this->attributes[$this->getKeyName()])) {
- return parent::delete();
- }
-
- return $this->getDeleteQuery()->delete();
- }
-
- /**
- * Get the query builder for a delete operation on the pivot.
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- protected function getDeleteQuery()
- {
- return $this->newQuery()->where([
- $this->foreignKey => $this->getOriginal($this->foreignKey, $this->getAttribute($this->foreignKey)),
- $this->relatedKey => $this->getOriginal($this->relatedKey, $this->getAttribute($this->relatedKey)),
- ]);
- }
-
- /**
- * Get the table associated with the model.
- *
- * @return string
- */
- public function getTable()
- {
- if (! isset($this->table)) {
- $this->setTable(str_replace(
- '\\', '', Str::snake(Str::singular(class_basename($this)))
- ));
- }
-
- return $this->table;
- }
-
- /**
- * Get the foreign key column name.
- *
- * @return string
- */
- public function getForeignKey()
- {
- return $this->foreignKey;
- }
-
- /**
- * Get the "related key" column name.
- *
- * @return string
- */
- public function getRelatedKey()
- {
- return $this->relatedKey;
- }
-
- /**
- * Get the "related key" column name.
- *
- * @return string
- */
- public function getOtherKey()
- {
- return $this->getRelatedKey();
- }
-
- /**
- * Set the key names for the pivot model instance.
- *
- * @param string $foreignKey
- * @param string $relatedKey
- * @return $this
- */
- public function setPivotKeys($foreignKey, $relatedKey)
- {
- $this->foreignKey = $foreignKey;
-
- $this->relatedKey = $relatedKey;
-
- 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->pivotParent)
- ? $this->pivotParent->getCreatedAtColumn()
- : parent::getCreatedAtColumn();
- }
-
- /**
- * Get the name of the "updated at" column.
- *
- * @return string
- */
- public function getUpdatedAtColumn()
- {
- return ($this->pivotParent)
- ? $this->pivotParent->getUpdatedAtColumn()
- : parent::getUpdatedAtColumn();
- }
-
- /**
- * Get the queueable identity for the entity.
- *
- * @return mixed
- */
- public function getQueueableId()
- {
- if (isset($this->attributes[$this->getKeyName()])) {
- return $this->getKey();
- }
-
- return sprintf(
- '%s:%s:%s:%s',
- $this->foreignKey, $this->getAttribute($this->foreignKey),
- $this->relatedKey, $this->getAttribute($this->relatedKey)
- );
- }
-
- /**
- * Get a new query to restore one or more models by their queueable IDs.
- *
- * @param array|int $ids
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function newQueryForRestoration($ids)
- {
- if (is_array($ids)) {
- return $this->newQueryForCollectionRestoration($ids);
- }
-
- if (! Str::contains($ids, ':')) {
- return parent::newQueryForRestoration($ids);
- }
-
- $segments = explode(':', $ids);
-
- return $this->newQueryWithoutScopes()
- ->where($segments[0], $segments[1])
- ->where($segments[2], $segments[3]);
- }
-
- /**
- * Get a new query to restore multiple models by their queueable IDs.
- *
- * @param array|int $ids
- * @return \Illuminate\Database\Eloquent\Builder
- */
- protected function newQueryForCollectionRestoration(array $ids)
- {
- if (! Str::contains($ids[0], ':')) {
- return parent::newQueryForRestoration($ids);
- }
-
- $query = $this->newQueryWithoutScopes();
-
- foreach ($ids as $id) {
- $segments = explode(':', $id);
-
- $query->orWhere(function ($query) use ($segments) {
- return $query->where($segments[0], $segments[1])
- ->where($segments[2], $segments[3]);
- });
- }
-
- return $query;
- }
- }
|