123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653 |
- <?php
- namespace Illuminate\Database\Eloquent;
- use Exception;
- use ArrayAccess;
- use JsonSerializable;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use Illuminate\Contracts\Support\Jsonable;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Support\Traits\ForwardsCalls;
- use Illuminate\Contracts\Routing\UrlRoutable;
- use Illuminate\Contracts\Queue\QueueableEntity;
- use Illuminate\Database\Eloquent\Relations\Pivot;
- use Illuminate\Contracts\Queue\QueueableCollection;
- use Illuminate\Support\Collection as BaseCollection;
- use Illuminate\Database\ConnectionResolverInterface as Resolver;
- abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
- {
- use Concerns\HasAttributes,
- Concerns\HasEvents,
- Concerns\HasGlobalScopes,
- Concerns\HasRelationships,
- Concerns\HasTimestamps,
- Concerns\HidesAttributes,
- Concerns\GuardsAttributes,
- ForwardsCalls;
- /**
- * The connection name for the model.
- *
- * @var string|null
- */
- protected $connection;
- /**
- * The table associated with the model.
- *
- * @var string
- */
- protected $table;
- /**
- * The primary key for the model.
- *
- * @var string
- */
- protected $primaryKey = 'id';
- /**
- * The "type" of the primary key ID.
- *
- * @var string
- */
- protected $keyType = 'int';
- /**
- * Indicates if the IDs are auto-incrementing.
- *
- * @var bool
- */
- public $incrementing = true;
- /**
- * The relations to eager load on every query.
- *
- * @var array
- */
- protected $with = [];
- /**
- * The relationship counts that should be eager loaded on every query.
- *
- * @var array
- */
- protected $withCount = [];
- /**
- * The number of models to return for pagination.
- *
- * @var int
- */
- protected $perPage = 15;
- /**
- * Indicates if the model exists.
- *
- * @var bool
- */
- public $exists = false;
- /**
- * Indicates if the model was inserted during the current request lifecycle.
- *
- * @var bool
- */
- public $wasRecentlyCreated = false;
- /**
- * The connection resolver instance.
- *
- * @var \Illuminate\Database\ConnectionResolverInterface
- */
- protected static $resolver;
- /**
- * The event dispatcher instance.
- *
- * @var \Illuminate\Contracts\Events\Dispatcher
- */
- protected static $dispatcher;
- /**
- * The array of booted models.
- *
- * @var array
- */
- protected static $booted = [];
- /**
- * The array of trait initializers that will be called on each new instance.
- *
- * @var array
- */
- protected static $traitInitializers = [];
- /**
- * The array of global scopes on the model.
- *
- * @var array
- */
- protected static $globalScopes = [];
- /**
- * The list of models classes that should not be affected with touch.
- *
- * @var array
- */
- protected static $ignoreOnTouch = [];
- /**
- * The name of the "created at" column.
- *
- * @var string
- */
- const CREATED_AT = 'created_at';
- /**
- * The name of the "updated at" column.
- *
- * @var string
- */
- const UPDATED_AT = 'updated_at';
- /**
- * Create a new Eloquent model instance.
- *
- * @param array $attributes
- * @return void
- */
- public function __construct(array $attributes = [])
- {
- $this->bootIfNotBooted();
- $this->initializeTraits();
- $this->syncOriginal();
- $this->fill($attributes);
- }
- /**
- * Check if the model needs to be booted and if so, do it.
- *
- * @return void
- */
- protected function bootIfNotBooted()
- {
- if (! isset(static::$booted[static::class])) {
- static::$booted[static::class] = true;
- $this->fireModelEvent('booting', false);
- static::boot();
- $this->fireModelEvent('booted', false);
- }
- }
- /**
- * The "booting" method of the model.
- *
- * @return void
- */
- protected static function boot()
- {
- static::bootTraits();
- }
- /**
- * Boot all of the bootable traits on the model.
- *
- * @return void
- */
- protected static function bootTraits()
- {
- $class = static::class;
- $booted = [];
- static::$traitInitializers[$class] = [];
- foreach (class_uses_recursive($class) as $trait) {
- $method = 'boot'.class_basename($trait);
- if (method_exists($class, $method) && ! in_array($method, $booted)) {
- forward_static_call([$class, $method]);
- $booted[] = $method;
- }
- if (method_exists($class, $method = 'initialize'.class_basename($trait))) {
- static::$traitInitializers[$class][] = $method;
- static::$traitInitializers[$class] = array_unique(
- static::$traitInitializers[$class]
- );
- }
- }
- }
- /**
- * Initialize any initializable traits on the model.
- *
- * @return void
- */
- protected function initializeTraits()
- {
- foreach (static::$traitInitializers[static::class] as $method) {
- $this->{$method}();
- }
- }
- /**
- * Clear the list of booted models so they will be re-booted.
- *
- * @return void
- */
- public static function clearBootedModels()
- {
- static::$booted = [];
- static::$globalScopes = [];
- }
- /**
- * Disables relationship model touching for the current class during given callback scope.
- *
- * @param callable $callback
- * @return void
- */
- public static function withoutTouching(callable $callback)
- {
- static::withoutTouchingOn([static::class], $callback);
- }
- /**
- * Disables relationship model touching for the given model classes during given callback scope.
- *
- * @param array $models
- * @param callable $callback
- * @return void
- */
- public static function withoutTouchingOn(array $models, callable $callback)
- {
- static::$ignoreOnTouch = array_values(array_merge(static::$ignoreOnTouch, $models));
- try {
- call_user_func($callback);
- } finally {
- static::$ignoreOnTouch = array_values(array_diff(static::$ignoreOnTouch, $models));
- }
- }
- /**
- * Determine if the given model is ignoring touches.
- *
- * @param string|null $class
- * @return bool
- */
- public static function isIgnoringTouch($class = null)
- {
- $class = $class ?: static::class;
- if (! get_class_vars($class)['timestamps'] || ! $class::UPDATED_AT) {
- return true;
- }
- foreach (static::$ignoreOnTouch as $ignoredClass) {
- if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) {
- return true;
- }
- }
- return false;
- }
- /**
- * Fill the model with an array of attributes.
- *
- * @param array $attributes
- * @return $this
- *
- * @throws \Illuminate\Database\Eloquent\MassAssignmentException
- */
- public function fill(array $attributes)
- {
- $totallyGuarded = $this->totallyGuarded();
- foreach ($this->fillableFromArray($attributes) as $key => $value) {
- $key = $this->removeTableFromKey($key);
- // The developers may choose to place some attributes in the "fillable" array
- // which means only those attributes may be set through mass assignment to
- // the model, and all others will just get ignored for security reasons.
- if ($this->isFillable($key)) {
- $this->setAttribute($key, $value);
- } elseif ($totallyGuarded) {
- throw new MassAssignmentException(sprintf(
- 'Add [%s] to fillable property to allow mass assignment on [%s].',
- $key, get_class($this)
- ));
- }
- }
- return $this;
- }
- /**
- * Fill the model with an array of attributes. Force mass assignment.
- *
- * @param array $attributes
- * @return $this
- */
- public function forceFill(array $attributes)
- {
- return static::unguarded(function () use ($attributes) {
- return $this->fill($attributes);
- });
- }
- /**
- * Qualify the given column name by the model's table.
- *
- * @param string $column
- * @return string
- */
- public function qualifyColumn($column)
- {
- if (Str::contains($column, '.')) {
- return $column;
- }
- return $this->getTable().'.'.$column;
- }
- /**
- * Remove the table name from a given key.
- *
- * @param string $key
- * @return string
- */
- protected function removeTableFromKey($key)
- {
- return Str::contains($key, '.') ? last(explode('.', $key)) : $key;
- }
- /**
- * Create a new instance of the given model.
- *
- * @param array $attributes
- * @param bool $exists
- * @return static
- */
- public function newInstance($attributes = [], $exists = false)
- {
- // This method just provides a convenient way for us to generate fresh model
- // instances of this current model. It is particularly useful during the
- // hydration of new objects via the Eloquent query builder instances.
- $model = new static((array) $attributes);
- $model->exists = $exists;
- $model->setConnection(
- $this->getConnectionName()
- );
- $model->setTable($this->getTable());
- return $model;
- }
- /**
- * Create a new model instance that is existing.
- *
- * @param array $attributes
- * @param string|null $connection
- * @return static
- */
- public function newFromBuilder($attributes = [], $connection = null)
- {
- $model = $this->newInstance([], true);
- $model->setRawAttributes((array) $attributes, true);
- $model->setConnection($connection ?: $this->getConnectionName());
- $model->fireModelEvent('retrieved', false);
- return $model;
- }
- /**
- * Begin querying the model on a given connection.
- *
- * @param string|null $connection
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public static function on($connection = null)
- {
- // First we will just create a fresh instance of this model, and then we can set the
- // connection on the model so that it is used for the queries we execute, as well
- // as being set on every relation we retrieve without a custom connection name.
- $instance = new static;
- $instance->setConnection($connection);
- return $instance->newQuery();
- }
- /**
- * Begin querying the model on the write connection.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- public static function onWriteConnection()
- {
- return static::query()->useWritePdo();
- }
- /**
- * Get all of the models from the database.
- *
- * @param array|mixed $columns
- * @return \Illuminate\Database\Eloquent\Collection|static[]
- */
- public static function all($columns = ['*'])
- {
- return static::query()->get(
- is_array($columns) ? $columns : func_get_args()
- );
- }
- /**
- * Begin querying a model with eager loading.
- *
- * @param array|string $relations
- * @return \Illuminate\Database\Eloquent\Builder|static
- */
- public static function with($relations)
- {
- return static::query()->with(
- is_string($relations) ? func_get_args() : $relations
- );
- }
- /**
- * Eager load relations on the model.
- *
- * @param array|string $relations
- * @return $this
- */
- public function load($relations)
- {
- $query = $this->newQueryWithoutRelationships()->with(
- is_string($relations) ? func_get_args() : $relations
- );
- $query->eagerLoadRelations([$this]);
- return $this;
- }
- /**
- * Eager load relations on the model if they are not already eager loaded.
- *
- * @param array|string $relations
- * @return $this
- */
- public function loadMissing($relations)
- {
- $relations = is_string($relations) ? func_get_args() : $relations;
- $this->newCollection([$this])->loadMissing($relations);
- return $this;
- }
- /**
- * Eager load relation counts on the model.
- *
- * @param array|string $relations
- * @return $this
- */
- public function loadCount($relations)
- {
- $relations = is_string($relations) ? func_get_args() : $relations;
- $this->newCollection([$this])->loadCount($relations);
- return $this;
- }
- /**
- * Increment a column's value by a given amount.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @return int
- */
- protected function increment($column, $amount = 1, array $extra = [])
- {
- return $this->incrementOrDecrement($column, $amount, $extra, 'increment');
- }
- /**
- * Decrement a column's value by a given amount.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @return int
- */
- protected function decrement($column, $amount = 1, array $extra = [])
- {
- return $this->incrementOrDecrement($column, $amount, $extra, 'decrement');
- }
- /**
- * Run the increment or decrement method on the model.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @param string $method
- * @return int
- */
- protected function incrementOrDecrement($column, $amount, $extra, $method)
- {
- $query = $this->newQueryWithoutRelationships();
- if (! $this->exists) {
- return $query->{$method}($column, $amount, $extra);
- }
- $this->incrementOrDecrementAttributeValue($column, $amount, $extra, $method);
- return $query->where(
- $this->getKeyName(), $this->getKey()
- )->{$method}($column, $amount, $extra);
- }
- /**
- * Increment the underlying attribute value and sync with original.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @param string $method
- * @return void
- */
- protected function incrementOrDecrementAttributeValue($column, $amount, $extra, $method)
- {
- $this->{$column} = $this->{$column} + ($method === 'increment' ? $amount : $amount * -1);
- $this->forceFill($extra);
- $this->syncOriginalAttribute($column);
- }
- /**
- * Update the model in the database.
- *
- * @param array $attributes
- * @param array $options
- * @return bool
- */
- public function update(array $attributes = [], array $options = [])
- {
- if (! $this->exists) {
- return false;
- }
- return $this->fill($attributes)->save($options);
- }
- /**
- * Save the model and all of its relationships.
- *
- * @return bool
- */
- public function push()
- {
- if (! $this->save()) {
- return false;
- }
- // To sync all of the relationships to the database, we will simply spin through
- // the relationships and save each model via this "push" method, which allows
- // us to recurse into all of these nested relations for the model instance.
- foreach ($this->relations as $models) {
- $models = $models instanceof Collection
- ? $models->all() : [$models];
- foreach (array_filter($models) as $model) {
- if (! $model->push()) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * Save the model to the database.
- *
- * @param array $options
- * @return bool
- */
- public function save(array $options = [])
- {
- $query = $this->newModelQuery();
- // If the "saving" event returns false we'll bail out of the save and return
- // false, indicating that the save failed. This provides a chance for any
- // listeners to cancel save operations if validations fail or whatever.
- if ($this->fireModelEvent('saving') === false) {
- return false;
- }
- // If the model already exists in the database we can just update our record
- // that is already in this database using the current IDs in this "where"
- // clause to only update this model. Otherwise, we'll just insert them.
- if ($this->exists) {
- $saved = $this->isDirty() ?
- $this->performUpdate($query) : true;
- }
- // If the model is brand new, we'll insert it into our database and set the
- // ID attribute on the model to the value of the newly inserted row's ID
- // which is typically an auto-increment value managed by the database.
- else {
- $saved = $this->performInsert($query);
- if (! $this->getConnectionName() &&
- $connection = $query->getConnection()) {
- $this->setConnection($connection->getName());
- }
- }
- // If the model is successfully saved, we need to do a few more things once
- // that is done. We will call the "saved" method here to run any actions
- // we need to happen after a model gets successfully saved right here.
- if ($saved) {
- $this->finishSave($options);
- }
- return $saved;
- }
- /**
- * Save the model to the database using transaction.
- *
- * @param array $options
- * @return bool
- *
- * @throws \Throwable
- */
- public function saveOrFail(array $options = [])
- {
- return $this->getConnection()->transaction(function () use ($options) {
- return $this->save($options);
- });
- }
- /**
- * Perform any actions that are necessary after the model is saved.
- *
- * @param array $options
- * @return void
- */
- protected function finishSave(array $options)
- {
- $this->fireModelEvent('saved', false);
- if ($this->isDirty() && ($options['touch'] ?? true)) {
- $this->touchOwners();
- }
- $this->syncOriginal();
- }
- /**
- * Perform a model update operation.
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return bool
- */
- protected function performUpdate(Builder $query)
- {
- // If the updating event returns false, we will cancel the update operation so
- // developers can hook Validation systems into their models and cancel this
- // operation if the model does not pass validation. Otherwise, we update.
- if ($this->fireModelEvent('updating') === false) {
- return false;
- }
- // First we need to create a fresh query instance and touch the creation and
- // update timestamp on the model which are maintained by us for developer
- // convenience. Then we will just continue saving the model instances.
- if ($this->usesTimestamps()) {
- $this->updateTimestamps();
- }
- // Once we have run the update operation, we will fire the "updated" event for
- // this model instance. This will allow developers to hook into these after
- // models are updated, giving them a chance to do any special processing.
- $dirty = $this->getDirty();
- if (count($dirty) > 0) {
- $this->setKeysForSaveQuery($query)->update($dirty);
- $this->syncChanges();
- $this->fireModelEvent('updated', false);
- }
- return true;
- }
- /**
- * Set the keys for a save update query.
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- protected function setKeysForSaveQuery(Builder $query)
- {
- $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
- return $query;
- }
- /**
- * Get the primary key value for a save query.
- *
- * @return mixed
- */
- protected function getKeyForSaveQuery()
- {
- return $this->original[$this->getKeyName()]
- ?? $this->getKey();
- }
- /**
- * Perform a model insert operation.
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return bool
- */
- protected function performInsert(Builder $query)
- {
- if ($this->fireModelEvent('creating') === false) {
- return false;
- }
- // First we'll need to create a fresh query instance and touch the creation and
- // update timestamps on this model, which are maintained by us for developer
- // convenience. After, we will just continue saving these model instances.
- if ($this->usesTimestamps()) {
- $this->updateTimestamps();
- }
- // If the model has an incrementing key, we can use the "insertGetId" method on
- // the query builder, which will give us back the final inserted ID for this
- // table from the database. Not all tables have to be incrementing though.
- $attributes = $this->getAttributes();
- if ($this->getIncrementing()) {
- $this->insertAndSetId($query, $attributes);
- }
- // If the table isn't incrementing we'll simply insert these attributes as they
- // are. These attribute arrays must contain an "id" column previously placed
- // there by the developer as the manually determined key for these models.
- else {
- if (empty($attributes)) {
- return true;
- }
- $query->insert($attributes);
- }
- // We will go ahead and set the exists property to true, so that it is set when
- // the created event is fired, just in case the developer tries to update it
- // during the event. This will allow them to do so and run an update here.
- $this->exists = true;
- $this->wasRecentlyCreated = true;
- $this->fireModelEvent('created', false);
- return true;
- }
- /**
- * Insert the given attributes and set the ID on the model.
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param array $attributes
- * @return void
- */
- protected function insertAndSetId(Builder $query, $attributes)
- {
- $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());
- $this->setAttribute($keyName, $id);
- }
- /**
- * Destroy the models for the given IDs.
- *
- * @param \Illuminate\Support\Collection|array|int $ids
- * @return int
- */
- public static function destroy($ids)
- {
- // We'll initialize a count here so we will return the total number of deletes
- // for the operation. The developers can then check this number as a boolean
- // type value or get this total count of records deleted for logging, etc.
- $count = 0;
- if ($ids instanceof BaseCollection) {
- $ids = $ids->all();
- }
- $ids = is_array($ids) ? $ids : func_get_args();
- // We will actually pull the models from the database table and call delete on
- // each of them individually so that their events get fired properly with a
- // correct set of attributes in case the developers wants to check these.
- $key = ($instance = new static)->getKeyName();
- foreach ($instance->whereIn($key, $ids)->get() as $model) {
- if ($model->delete()) {
- $count++;
- }
- }
- return $count;
- }
- /**
- * Delete the model from the database.
- *
- * @return bool|null
- *
- * @throws \Exception
- */
- public function delete()
- {
- if (is_null($this->getKeyName())) {
- throw new Exception('No primary key defined on model.');
- }
- // If the model doesn't exist, there is nothing to delete so we'll just return
- // immediately and not do anything else. Otherwise, we will continue with a
- // deletion process on the model, firing the proper events, and so forth.
- if (! $this->exists) {
- return;
- }
- if ($this->fireModelEvent('deleting') === false) {
- return false;
- }
- // Here, we'll touch the owning models, verifying these timestamps get updated
- // for the models. This will allow any caching to get broken on the parents
- // by the timestamp. Then we will go ahead and delete the model instance.
- $this->touchOwners();
- $this->performDeleteOnModel();
- // Once the model has been deleted, we will fire off the deleted event so that
- // the developers may hook into post-delete operations. We will then return
- // a boolean true as the delete is presumably successful on the database.
- $this->fireModelEvent('deleted', false);
- return true;
- }
- /**
- * Force a hard delete on a soft deleted model.
- *
- * This method protects developers from running forceDelete when trait is missing.
- *
- * @return bool|null
- */
- public function forceDelete()
- {
- return $this->delete();
- }
- /**
- * Perform the actual delete query on this model instance.
- *
- * @return void
- */
- protected function performDeleteOnModel()
- {
- $this->setKeysForSaveQuery($this->newModelQuery())->delete();
- $this->exists = false;
- }
- /**
- * Begin querying the model.
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public static function query()
- {
- return (new static)->newQuery();
- }
- /**
- * Get a new query builder for the model's table.
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function newQuery()
- {
- return $this->registerGlobalScopes($this->newQueryWithoutScopes());
- }
- /**
- * Get a new query builder that doesn't have any global scopes or eager loading.
- *
- * @return \Illuminate\Database\Eloquent\Builder|static
- */
- public function newModelQuery()
- {
- return $this->newEloquentBuilder(
- $this->newBaseQueryBuilder()
- )->setModel($this);
- }
- /**
- * Get a new query builder with no relationships loaded.
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function newQueryWithoutRelationships()
- {
- return $this->registerGlobalScopes($this->newModelQuery());
- }
- /**
- * Register the global scopes for this builder instance.
- *
- * @param \Illuminate\Database\Eloquent\Builder $builder
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function registerGlobalScopes($builder)
- {
- foreach ($this->getGlobalScopes() as $identifier => $scope) {
- $builder->withGlobalScope($identifier, $scope);
- }
- return $builder;
- }
- /**
- * Get a new query builder that doesn't have any global scopes.
- *
- * @return \Illuminate\Database\Eloquent\Builder|static
- */
- public function newQueryWithoutScopes()
- {
- return $this->newModelQuery()
- ->with($this->with)
- ->withCount($this->withCount);
- }
- /**
- * Get a new query instance without a given scope.
- *
- * @param \Illuminate\Database\Eloquent\Scope|string $scope
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function newQueryWithoutScope($scope)
- {
- return $this->newQuery()->withoutGlobalScope($scope);
- }
- /**
- * 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)
- {
- return is_array($ids)
- ? $this->newQueryWithoutScopes()->whereIn($this->getQualifiedKeyName(), $ids)
- : $this->newQueryWithoutScopes()->whereKey($ids);
- }
- /**
- * Create a new Eloquent query builder for the model.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder|static
- */
- public function newEloquentBuilder($query)
- {
- return new Builder($query);
- }
- /**
- * Get a new query builder instance for the connection.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- protected function newBaseQueryBuilder()
- {
- return $this->getConnection()->query();
- }
- /**
- * Create a new Eloquent Collection instance.
- *
- * @param array $models
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function newCollection(array $models = [])
- {
- return new Collection($models);
- }
- /**
- * Create a new pivot model instance.
- *
- * @param \Illuminate\Database\Eloquent\Model $parent
- * @param array $attributes
- * @param string $table
- * @param bool $exists
- * @param string|null $using
- * @return \Illuminate\Database\Eloquent\Relations\Pivot
- */
- public function newPivot(self $parent, array $attributes, $table, $exists, $using = null)
- {
- return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists)
- : Pivot::fromAttributes($parent, $attributes, $table, $exists);
- }
- /**
- * Convert the model instance to an array.
- *
- * @return array
- */
- public function toArray()
- {
- return array_merge($this->attributesToArray(), $this->relationsToArray());
- }
- /**
- * Convert the model instance to JSON.
- *
- * @param int $options
- * @return string
- *
- * @throws \Illuminate\Database\Eloquent\JsonEncodingException
- */
- public function toJson($options = 0)
- {
- $json = json_encode($this->jsonSerialize(), $options);
- if (JSON_ERROR_NONE !== json_last_error()) {
- throw JsonEncodingException::forModel($this, json_last_error_msg());
- }
- return $json;
- }
- /**
- * Convert the object into something JSON serializable.
- *
- * @return array
- */
- public function jsonSerialize()
- {
- return $this->toArray();
- }
- /**
- * Reload a fresh model instance from the database.
- *
- * @param array|string $with
- * @return static|null
- */
- public function fresh($with = [])
- {
- if (! $this->exists) {
- return;
- }
- return static::newQueryWithoutScopes()
- ->with(is_string($with) ? func_get_args() : $with)
- ->where($this->getKeyName(), $this->getKey())
- ->first();
- }
- /**
- * Reload the current model instance with fresh attributes from the database.
- *
- * @return $this
- */
- public function refresh()
- {
- if (! $this->exists) {
- return $this;
- }
- $this->setRawAttributes(
- static::newQueryWithoutScopes()->findOrFail($this->getKey())->attributes
- );
- $this->load(collect($this->relations)->except('pivot')->keys()->toArray());
- $this->syncOriginal();
- return $this;
- }
- /**
- * Clone the model into a new, non-existing instance.
- *
- * @param array|null $except
- * @return static
- */
- public function replicate(array $except = null)
- {
- $defaults = [
- $this->getKeyName(),
- $this->getCreatedAtColumn(),
- $this->getUpdatedAtColumn(),
- ];
- $attributes = Arr::except(
- $this->attributes, $except ? array_unique(array_merge($except, $defaults)) : $defaults
- );
- return tap(new static, function ($instance) use ($attributes) {
- $instance->setRawAttributes($attributes);
- $instance->setRelations($this->relations);
- $instance->fireModelEvent('replicating', false);
- });
- }
- /**
- * Determine if two models have the same ID and belong to the same table.
- *
- * @param \Illuminate\Database\Eloquent\Model|null $model
- * @return bool
- */
- public function is($model)
- {
- return ! is_null($model) &&
- $this->getKey() === $model->getKey() &&
- $this->getTable() === $model->getTable() &&
- $this->getConnectionName() === $model->getConnectionName();
- }
- /**
- * Determine if two models are not the same.
- *
- * @param \Illuminate\Database\Eloquent\Model|null $model
- * @return bool
- */
- public function isNot($model)
- {
- return ! $this->is($model);
- }
- /**
- * Get the database connection for the model.
- *
- * @return \Illuminate\Database\Connection
- */
- public function getConnection()
- {
- return static::resolveConnection($this->getConnectionName());
- }
- /**
- * Get the current connection name for the model.
- *
- * @return string|null
- */
- public function getConnectionName()
- {
- return $this->connection;
- }
- /**
- * Set the connection associated with the model.
- *
- * @param string|null $name
- * @return $this
- */
- public function setConnection($name)
- {
- $this->connection = $name;
- return $this;
- }
- /**
- * Resolve a connection instance.
- *
- * @param string|null $connection
- * @return \Illuminate\Database\Connection
- */
- public static function resolveConnection($connection = null)
- {
- return static::$resolver->connection($connection);
- }
- /**
- * Get the connection resolver instance.
- *
- * @return \Illuminate\Database\ConnectionResolverInterface
- */
- public static function getConnectionResolver()
- {
- return static::$resolver;
- }
- /**
- * Set the connection resolver instance.
- *
- * @param \Illuminate\Database\ConnectionResolverInterface $resolver
- * @return void
- */
- public static function setConnectionResolver(Resolver $resolver)
- {
- static::$resolver = $resolver;
- }
- /**
- * Unset the connection resolver for models.
- *
- * @return void
- */
- public static function unsetConnectionResolver()
- {
- static::$resolver = null;
- }
- /**
- * Get the table associated with the model.
- *
- * @return string
- */
- public function getTable()
- {
- return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this)));
- }
- /**
- * Set the table associated with the model.
- *
- * @param string $table
- * @return $this
- */
- public function setTable($table)
- {
- $this->table = $table;
- return $this;
- }
- /**
- * Get the primary key for the model.
- *
- * @return string
- */
- public function getKeyName()
- {
- return $this->primaryKey;
- }
- /**
- * Set the primary key for the model.
- *
- * @param string $key
- * @return $this
- */
- public function setKeyName($key)
- {
- $this->primaryKey = $key;
- return $this;
- }
- /**
- * Get the table qualified key name.
- *
- * @return string
- */
- public function getQualifiedKeyName()
- {
- return $this->qualifyColumn($this->getKeyName());
- }
- /**
- * Get the auto-incrementing key type.
- *
- * @return string
- */
- public function getKeyType()
- {
- return $this->keyType;
- }
- /**
- * Set the data type for the primary key.
- *
- * @param string $type
- * @return $this
- */
- public function setKeyType($type)
- {
- $this->keyType = $type;
- return $this;
- }
- /**
- * Get the value indicating whether the IDs are incrementing.
- *
- * @return bool
- */
- public function getIncrementing()
- {
- return $this->incrementing;
- }
- /**
- * Set whether IDs are incrementing.
- *
- * @param bool $value
- * @return $this
- */
- public function setIncrementing($value)
- {
- $this->incrementing = $value;
- return $this;
- }
- /**
- * Get the value of the model's primary key.
- *
- * @return mixed
- */
- public function getKey()
- {
- return $this->getAttribute($this->getKeyName());
- }
- /**
- * Get the queueable identity for the entity.
- *
- * @return mixed
- */
- public function getQueueableId()
- {
- return $this->getKey();
- }
- /**
- * Get the queueable relationships for the entity.
- *
- * @return array
- */
- public function getQueueableRelations()
- {
- $relations = [];
- foreach ($this->getRelations() as $key => $relation) {
- if (! method_exists($this, $key)) {
- continue;
- }
- $relations[] = $key;
- if ($relation instanceof QueueableCollection) {
- foreach ($relation->getQueueableRelations() as $collectionValue) {
- $relations[] = $key.'.'.$collectionValue;
- }
- }
- if ($relation instanceof QueueableEntity) {
- foreach ($relation->getQueueableRelations() as $entityKey => $entityValue) {
- $relations[] = $key.'.'.$entityValue;
- }
- }
- }
- return array_unique($relations);
- }
- /**
- * Get the queueable connection for the entity.
- *
- * @return string|null
- */
- public function getQueueableConnection()
- {
- return $this->getConnectionName();
- }
- /**
- * Get the value of the model's route key.
- *
- * @return mixed
- */
- public function getRouteKey()
- {
- return $this->getAttribute($this->getRouteKeyName());
- }
- /**
- * Get the route key for the model.
- *
- * @return string
- */
- public function getRouteKeyName()
- {
- return $this->getKeyName();
- }
- /**
- * Retrieve the model for a bound value.
- *
- * @param mixed $value
- * @return \Illuminate\Database\Eloquent\Model|null
- */
- public function resolveRouteBinding($value)
- {
- return $this->where($this->getRouteKeyName(), $value)->first();
- }
- /**
- * Get the default foreign key name for the model.
- *
- * @return string
- */
- public function getForeignKey()
- {
- return Str::snake(class_basename($this)).'_'.$this->getKeyName();
- }
- /**
- * Get the number of models to return per page.
- *
- * @return int
- */
- public function getPerPage()
- {
- return $this->perPage;
- }
- /**
- * Set the number of models to return per page.
- *
- * @param int $perPage
- * @return $this
- */
- public function setPerPage($perPage)
- {
- $this->perPage = $perPage;
- return $this;
- }
- /**
- * Dynamically retrieve attributes on the model.
- *
- * @param string $key
- * @return mixed
- */
- public function __get($key)
- {
- return $this->getAttribute($key);
- }
- /**
- * Dynamically set attributes on the model.
- *
- * @param string $key
- * @param mixed $value
- * @return void
- */
- public function __set($key, $value)
- {
- $this->setAttribute($key, $value);
- }
- /**
- * Determine if the given attribute exists.
- *
- * @param mixed $offset
- * @return bool
- */
- public function offsetExists($offset)
- {
- return ! is_null($this->getAttribute($offset));
- }
- /**
- * Get the value for a given offset.
- *
- * @param mixed $offset
- * @return mixed
- */
- public function offsetGet($offset)
- {
- return $this->getAttribute($offset);
- }
- /**
- * Set the value for a given offset.
- *
- * @param mixed $offset
- * @param mixed $value
- * @return void
- */
- public function offsetSet($offset, $value)
- {
- $this->setAttribute($offset, $value);
- }
- /**
- * Unset the value for a given offset.
- *
- * @param mixed $offset
- * @return void
- */
- public function offsetUnset($offset)
- {
- unset($this->attributes[$offset], $this->relations[$offset]);
- }
- /**
- * Determine if an attribute or relation exists on the model.
- *
- * @param string $key
- * @return bool
- */
- public function __isset($key)
- {
- return $this->offsetExists($key);
- }
- /**
- * Unset an attribute on the model.
- *
- * @param string $key
- * @return void
- */
- public function __unset($key)
- {
- $this->offsetUnset($key);
- }
- /**
- * Handle dynamic method calls into the model.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- if (in_array($method, ['increment', 'decrement'])) {
- return $this->$method(...$parameters);
- }
- return $this->forwardCallTo($this->newQuery(), $method, $parameters);
- }
- /**
- * Handle dynamic static method calls into the method.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public static function __callStatic($method, $parameters)
- {
- return (new static)->$method(...$parameters);
- }
- /**
- * Convert the model to its string representation.
- *
- * @return string
- */
- public function __toString()
- {
- return $this->toJson();
- }
- /**
- * When a model is being unserialized, check if it needs to be booted.
- *
- * @return void
- */
- public function __wakeup()
- {
- $this->bootIfNotBooted();
- }
- }
|