| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504 |
- <?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\Contracts\Routing\UrlRoutable;
- use Illuminate\Contracts\Queue\QueueableEntity;
- use Illuminate\Database\Eloquent\Relations\Pivot;
- use Illuminate\Database\Query\Builder as QueryBuilder;
- 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;
- /**
- * The connection name for the model.
- *
- * @var string
- */
- 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 auto-incrementing 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 global scopes on the model.
- *
- * @var array
- */
- protected static $globalScopes = [];
- /**
- * 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->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;
- foreach (class_uses_recursive($class) as $trait) {
- if (method_exists($class, $method = 'boot'.class_basename($trait))) {
- forward_static_call([$class, $method]);
- }
- }
- }
- /**
- * Clear the list of booted models so they will be re-booted.
- *
- * @return void
- */
- public static function clearBootedModels()
- {
- static::$booted = [];
- static::$globalScopes = [];
- }
- /**
- * 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($key);
- }
- }
- 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()
- );
- 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 be used for the queries
- // we execute, as well as being set on each relationship we retrieve.
- $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()
- {
- $instance = new static;
- return $instance->newQuery()->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 (new static)->newQuery()->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 (new static)->newQuery()->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;
- return $this->load(array_filter($relations, function ($relation) {
- return ! $this->relationLoaded($relation);
- }));
- }
- /**
- * Increment a column's value by a given amount.
- *
- * @param string $column
- * @param 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 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 int $amount
- * @param array $extra
- * @param string $method
- * @return int
- */
- protected function incrementOrDecrement($column, $amount, $extra, $method)
- {
- $query = $this->newQuery();
- 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 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->newQueryWithoutScopes();
- // 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->fireModelEvent('updated', false);
- $this->syncChanges();
- }
- 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->attributes;
- 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 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;
- $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->newQueryWithoutScopes())->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 with no relationships loaded.
- *
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function newQueryWithoutRelationships()
- {
- return $this->registerGlobalScopes(
- $this->newEloquentBuilder($this->newBaseQueryBuilder())->setModel($this)
- );
- }
- /**
- * 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()
- {
- $builder = $this->newEloquentBuilder($this->newBaseQueryBuilder());
- // Once we have the query builders, we will set the model instances so the
- // builder can easily access any information it may need from the model
- // while it is constructing and executing various queries against it.
- return $builder->setModel($this)
- ->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)
- {
- $builder = $this->newQuery();
- return $builder->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)
- {
- if (is_array($ids)) {
- return $this->newQueryWithoutScopes()->whereIn($this->getQualifiedKeyName(), $ids);
- }
- return $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()
- {
- $connection = $this->getConnection();
- return new QueryBuilder(
- $connection, $connection->getQueryGrammar(), $connection->getPostProcessor()
- );
- }
- /**
- * 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());
- return $this;
- }
- /**
- * Clone the model into a new, non-existing instance.
- *
- * @param array|null $except
- * @return \Illuminate\Database\Eloquent\Model
- */
- 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);
- });
- }
- /**
- * 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
- */
- public function getConnectionName()
- {
- return $this->connection;
- }
- /**
- * Set the connection associated with the model.
- *
- * @param string $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()
- {
- if (! isset($this->table)) {
- return str_replace(
- '\\', '', Str::snake(Str::plural(class_basename($this)))
- );
- }
- return $this->table;
- }
- /**
- * 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 connection for the entity.
- *
- * @return mixed
- */
- 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->primaryKey;
- }
- /**
- * 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->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();
- }
- }
|