You're browsing the documentation for an upcoming version of Laravel. The documentation and features of this release are subject to change.
abstract

Model

Implements

Uses

Constants

public

CREATED_AT string|null

The name of the "created at" column.

Default: 'created_at'
public

UPDATED_AT string|null

The name of the "updated at" column.

Default: 'updated_at'

Properties

protected

$connection string|null

The connection name for the model.

Default: null
protected

$table string

The table associated with the model.

Default: null
protected

$primaryKey string

The primary key for the model.

Default: 'id'
protected

$keyType string

The "type" of the primary key ID.

Default: 'int'
public

$incrementing bool

Indicates if the IDs are auto-incrementing.

Default: true
protected

$with array

The relations to eager load on every query.

Default: []
protected

$withCount array

The relationship counts that should be eager loaded on every query.

Default: []
public

$preventsLazyLoading bool

Indicates whether lazy loading will be prevented on this model.

Default: false
protected

$perPage int

The number of models to return for pagination.

Default: 15
public

$exists bool

Indicates if the model exists.

Default: false
public

$wasRecentlyCreated bool

Indicates if the model was inserted during the current request lifecycle.

Default: false
protected

$escapeWhenCastingToString bool

Indicates that the object's string representation should be escaped when __toString is invoked.

Default: false
protected static

$resolver ConnectionResolverInterface

The connection resolver instance.

Default: null
protected static

$dispatcher Dispatcher

The event dispatcher instance.

Default: null
protected static

$booted array

The array of booted models.

Default: []
protected static

$traitInitializers array

The array of trait initializers that will be called on each new instance.

Default: []
protected static

$globalScopes array

The array of global scopes on the model.

Default: []
protected static

$ignoreOnTouch array

The list of models classes that should not be affected with touch.

Default: []
protected static

$modelsShouldPreventLazyLoading bool

Indicates whether lazy loading should be restricted on all models.

Default: false
protected static

$lazyLoadingViolationCallback callable|null

The callback that is responsible for handling lazy loading violations.

Default: null
protected static

$modelsShouldPreventSilentlyDiscardingAttributes bool

Indicates if an exception should be thrown instead of silently discarding non-fillable attributes.

Default: false
protected static

$discardedAttributeViolationCallback callable|null

The callback that is responsible for handling discarded attribute violations.

Default: null
protected static

$modelsShouldPreventAccessingMissingAttributes bool

Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model.

Default: false
protected static

$missingAttributeViolationCallback callable|null

The callback that is responsible for handling missing attribute violations.

Default: null
protected static

$isBroadcasting bool

Indicates if broadcasting is currently enabled.

Default: true

Methods

public

__construct ( array $attributes ) : void

Create a new Eloquent model instance.

Parameters

  • $attributes array
protected

bootIfNotBooted ( ) : void

Check if the model needs to be booted and if so, do it.

protected static

booting ( ) : void

Perform any actions required before the model boots.

protected static

boot ( ) : void

Bootstrap the model and its traits.

protected static

bootTraits ( ) : void

Boot all of the bootable traits on the model.

protected

initializeTraits ( ) : void

Initialize any initializable traits on the model.

protected static

booted ( ) : void

Perform any actions required after the model boots.

public static

clearBootedModels ( ) : void

Clear the list of booted models so they will be re-booted.

public static

withoutTouching ( callable $callback ) : void

Disables relationship model touching for the current class during given callback scope.

Parameters

  • $callback callable
public static

withoutTouchingOn ( array $models , callable $callback ) : void

Disables relationship model touching for the given model classes during given callback scope.

Parameters

  • $models array
  • $callback callable
public static

isIgnoringTouch ( $class ) : bool

Determine if the given model is ignoring touches.

Parameters

  • $class string|null
public static

shouldBeStrict ( bool $shouldBeStrict ) : void

Indicate that models should prevent lazy loading, silently discarding attributes, and accessing missing attributes.

Parameters

  • $shouldBeStrict bool
public static

preventLazyLoading ( $value ) : void

Prevent model relationships from being lazy loaded.

Parameters

  • $value bool
public static

handleLazyLoadingViolationUsing ( callable $callback ) : void

Register a callback that is responsible for handling lazy loading violations.

Parameters

  • $callback callable|null
public static

preventSilentlyDiscardingAttributes ( $value ) : void

Prevent non-fillable attributes from being silently discarded.

Parameters

  • $value bool
public static

handleDiscardedAttributeViolationUsing ( callable $callback ) : void

Register a callback that is responsible for handling discarded attribute violations.

Parameters

  • $callback callable|null
public static

preventAccessingMissingAttributes ( $value ) : void

Prevent accessing missing attributes on retrieved models.

Parameters

  • $value bool
public static

handleMissingAttributeViolationUsing ( callable $callback ) : void

Register a callback that is responsible for handling missing attribute violations.

Parameters

  • $callback callable|null
public static

withoutBroadcasting ( callable $callback ) : mixed

Execute a callback without broadcasting any model events for all model types.

Parameters

  • $callback callable
public

fill ( array $attributes ) : $this

Fill the model with an array of attributes.

Parameters

  • $attributes array
public

forceFill ( array $attributes ) : $this

Fill the model with an array of attributes. Force mass assignment.

Parameters

  • $attributes array
public

qualifyColumn ( $column ) : string

Qualify the given column name by the model's table.

Parameters

  • $column string
public

qualifyColumns ( $columns ) : array

Qualify the given columns with the model's table.

Parameters

  • $columns array
public

newInstance ( $attributes , $exists ) : static

Create a new instance of the given model.

Parameters

  • $attributes array
  • $exists bool
public

newFromBuilder ( $attributes , $connection ) : static

Create a new model instance that is existing.

Parameters

  • $attributes array
  • $connection string|null
public static

on ( $connection ) : Builder

Begin querying the model on a given connection.

Parameters

  • $connection string|null
public static

onWriteConnection ( ) : Builder

Begin querying the model on the write connection.

public static

all ( $columns ) : Illuminate\...\Collection

Get all of the models from the database.

Parameters

  • $columns array|string
public static

with ( $relations ) : Builder

Begin querying a model with eager loading.

Parameters

  • $relations array|string
public

load ( $relations ) : $this

Eager load relations on the model.

Parameters

  • $relations array|string
public

loadMorph ( $relation , $relations ) : $this

Eager load relationships on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
public

loadMissing ( $relations ) : $this

Eager load relations on the model if they are not already eager loaded.

Parameters

  • $relations array|string
public

loadAggregate ( $relations , $column , $function ) : $this

Eager load relation's column aggregations on the model.

Parameters

  • $relations array|string
  • $column string
  • $function string|null
public

loadCount ( $relations ) : $this

Eager load relation counts on the model.

Parameters

  • $relations array|string
public

loadMax ( $relations , $column ) : $this

Eager load relation max column values on the model.

Parameters

  • $relations array|string
  • $column string
public

loadMin ( $relations , $column ) : $this

Eager load relation min column values on the model.

Parameters

  • $relations array|string
  • $column string
public

loadSum ( $relations , $column ) : $this

Eager load relation's column summations on the model.

Parameters

  • $relations array|string
  • $column string
public

loadAvg ( $relations , $column ) : $this

Eager load relation average column values on the model.

Parameters

  • $relations array|string
  • $column string
public

loadExists ( $relations ) : $this

Eager load related model existence values on the model.

Parameters

  • $relations array|string
public

loadMorphAggregate ( $relation , $relations , $column , $function ) : $this

Eager load relationship column aggregation on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
  • $column string
  • $function string|null
public

loadMorphCount ( $relation , $relations ) : $this

Eager load relationship counts on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
public

loadMorphMax ( $relation , $relations , $column ) : $this

Eager load relationship max column values on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
  • $column string
public

loadMorphMin ( $relation , $relations , $column ) : $this

Eager load relationship min column values on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
  • $column string
public

loadMorphSum ( $relation , $relations , $column ) : $this

Eager load relationship column summations on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
  • $column string
public

loadMorphAvg ( $relation , $relations , $column ) : $this

Eager load relationship average column values on the polymorphic relation of a model.

Parameters

  • $relation string
  • $relations array
  • $column string
protected

increment ( $column , $amount , array $extra ) : int

Increment a column's value by a given amount.

Parameters

  • $column string
  • $amount float|int
  • $extra array
protected

decrement ( $column , $amount , array $extra ) : int

Decrement a column's value by a given amount.

Parameters

  • $column string
  • $amount float|int
  • $extra array
protected

incrementOrDecrement ( $column , $amount , $extra , $method ) : int

Run the increment or decrement method on the model.

Parameters

  • $column string
  • $amount float|int
  • $extra array
  • $method string
public

update ( array $attributes , array $options ) : bool

Update the model in the database.

Parameters

  • $attributes array
  • $options array
public

updateOrFail ( array $attributes , array $options ) : bool

Update the model in the database within a transaction.

Parameters

  • $attributes array
  • $options array
public

updateQuietly ( array $attributes , array $options ) : bool

Update the model in the database without raising any events.

Parameters

  • $attributes array
  • $options array
protected

incrementQuietly ( $column , $amount , array $extra ) : int

Increment a column's value by a given amount without raising any events.

Parameters

  • $column string
  • $amount float|int
  • $extra array
protected

decrementQuietly ( $column , $amount , array $extra ) : int

Decrement a column's value by a given amount without raising any events.

Parameters

  • $column string
  • $amount float|int
  • $extra array
public

push ( ) : bool

Save the model and all of its relationships.

public

pushQuietly ( ) : bool

Save the model and all of its relationships without raising any events to the parent model.

public

saveQuietly ( array $options ) : bool

Save the model to the database without raising any events.

Parameters

  • $options array
public

save ( array $options ) : bool

Save the model to the database.

Parameters

  • $options array
public

saveOrFail ( array $options ) : bool

Save the model to the database within a transaction.

Parameters

  • $options array
protected

finishSave ( array $options ) : void

Perform any actions that are necessary after the model is saved.

Parameters

  • $options array
protected

performUpdate ( Builder $query ) : bool

Perform a model update operation.

Parameters

protected

setKeysForSelectQuery ( $query ) : Builder

Set the keys for a select query.

Parameters

protected

getKeyForSelectQuery ( ) : mixed

Get the primary key value for a select query.

protected

setKeysForSaveQuery ( $query ) : Builder

Set the keys for a save update query.

Parameters

protected

getKeyForSaveQuery ( ) : mixed

Get the primary key value for a save query.

protected

performInsert ( Builder $query ) : bool

Perform a model insert operation.

Parameters

protected

insertAndSetId ( Builder $query , $attributes ) : void

Insert the given attributes and set the ID on the model.

Parameters

public static

destroy ( $ids ) : int

Destroy the models for the given IDs.

Parameters

  • $ids Illuminate\...\Collection|array|int|string
public

delete ( ) : bool|null

Delete the model from the database.

public

deleteQuietly ( ) : bool

Delete the model from the database without raising any events.

public

deleteOrFail ( ) : bool|null

Delete the model from the database within a transaction.

public

forceDelete ( ) : bool|null

Force a hard delete on a soft deleted model.

protected

performDeleteOnModel ( ) : void

Perform the actual delete query on this model instance.

public static

query ( ) : Builder

Begin querying the model.

public

newQuery ( ) : Builder

Get a new query builder for the model's table.

public

newModelQuery ( ) : Builder |static

Get a new query builder that doesn't have any global scopes or eager loading.

public

newQueryWithoutRelationships ( ) : Builder

Get a new query builder with no relationships loaded.

public

registerGlobalScopes ( $builder ) : Builder

Register the global scopes for this builder instance.

Parameters

public

newQueryWithoutScopes ( ) : Builder |static

Get a new query builder that doesn't have any global scopes.

public

newQueryWithoutScope ( $scope ) : Builder

Get a new query instance without a given scope.

Parameters

public

newQueryForRestoration ( $ids ) : Builder

Get a new query to restore one or more models by their queueable IDs.

Parameters

  • $ids array|int
public

newEloquentBuilder ( $query ) : Builder |static

Create a new Eloquent query builder for the model.

Parameters

protected

newBaseQueryBuilder ( ) : Builder

Get a new query builder instance for the connection.

public

newCollection ( array $models ) : Illuminate\...\Collection

Create a new Eloquent Collection instance.

Parameters

  • $models array
public

newPivot ( $parent , array $attributes , $table , $exists , $using ) : Pivot

Create a new pivot model instance.

Parameters

  • $parent Model
  • $attributes array
  • $table string
  • $exists bool
  • $using string|null
public

hasNamedScope ( $scope ) : bool

Determine if the model has a given scope.

Parameters

  • $scope string
public

callNamedScope ( $scope , array $parameters ) : mixed

Apply the given named scope if possible.

Parameters

  • $scope string
  • $parameters array
public

toArray ( ) : array

Convert the model instance to an array.

public

toJson ( $options ) : string

Convert the model instance to JSON.

Parameters

  • $options int
public

jsonSerialize ( ) : mixed

Convert the object into something JSON serializable.

public

fresh ( $with ) : static|null

Reload a fresh model instance from the database.

Parameters

  • $with array|string
public

refresh ( ) : $this

Reload the current model instance with fresh attributes from the database.

public

replicate ( array $except ) : static

Clone the model into a new, non-existing instance.

Parameters

  • $except array|null
public

replicateQuietly ( array $except ) : static

Clone the model into a new, non-existing instance without raising any events.

Parameters

  • $except array|null
public

is ( $model ) : bool

Determine if two models have the same ID and belong to the same table.

Parameters

public

isNot ( $model ) : bool

Determine if two models are not the same.

Parameters

public

getConnection ( ) : Connection

Get the database connection for the model.

public

getConnectionName ( ) : string|null

Get the current connection name for the model.

public

setConnection ( $name ) : $this

Set the connection associated with the model.

Parameters

  • $name string|null
public static

resolveConnection ( $connection ) : Connection

Resolve a connection instance.

Parameters

  • $connection string|null
public static

getConnectionResolver ( ) : ConnectionResolverInterface |null

Get the connection resolver instance.

public static

setConnectionResolver ( ConnectionResolverInterface $resolver ) : void

Set the connection resolver instance.

Parameters

public static

unsetConnectionResolver ( ) : void

Unset the connection resolver for models.

public

getTable ( ) : string

Get the table associated with the model.

public

setTable ( $table ) : $this

Set the table associated with the model.

Parameters

  • $table string
public

getKeyName ( ) : string

Get the primary key for the model.

public

setKeyName ( $key ) : $this

Set the primary key for the model.

Parameters

  • $key string
public

getQualifiedKeyName ( ) : string

Get the table qualified key name.

public

getKeyType ( ) : string

Get the auto-incrementing key type.

public

setKeyType ( $type ) : $this

Set the data type for the primary key.

Parameters

  • $type string
public

getIncrementing ( ) : bool

Get the value indicating whether the IDs are incrementing.

public

setIncrementing ( $value ) : $this

Set whether IDs are incrementing.

Parameters

  • $value bool
public

getKey ( ) : mixed

Get the value of the model's primary key.

public

getQueueableId ( ) : mixed

Get the queueable identity for the entity.

public

getQueueableRelations ( ) : array

Get the queueable relationships for the entity.

public

getQueueableConnection ( ) : string|null

Get the queueable connection for the entity.

public

getRouteKey ( ) : mixed

Get the value of the model's route key.

public

getRouteKeyName ( ) : string

Get the route key for the model.

public

resolveRouteBinding ( $value , $field ) : Model |null

Retrieve the model for a bound value.

Parameters

  • $value mixed
  • $field string|null
public

resolveSoftDeletableRouteBinding ( $value , $field ) : Model |null

Retrieve the model for a bound value.

Parameters

  • $value mixed
  • $field string|null
public

resolveChildRouteBinding ( $childType , $value , $field ) : Model |null

Retrieve the child model for a bound value.

Parameters

  • $childType string
  • $value mixed
  • $field string|null
public

resolveSoftDeletableChildRouteBinding ( $childType , $value , $field ) : Model |null

Retrieve the child model for a bound value.

Parameters

  • $childType string
  • $value mixed
  • $field string|null
protected

resolveChildRouteBindingQuery ( $childType , $value , $field ) : Relation

Retrieve the child model query for a bound value.

Parameters

  • $childType string
  • $value mixed
  • $field string|null
protected

childRouteBindingRelationshipName ( $childType ) : string

Retrieve the child route model binding relationship name for the given child type.

Parameters

  • $childType string
public

resolveRouteBindingQuery ( $query , $value , $field ) : Relation

Retrieve the model for a bound value.

Parameters

public

getForeignKey ( ) : string

Get the default foreign key name for the model.

public

getPerPage ( ) : int

Get the number of models to return per page.

public

setPerPage ( $perPage ) : $this

Set the number of models to return per page.

Parameters

  • $perPage int
public static

preventsLazyLoading ( ) : bool

Determine if lazy loading is disabled.

public static

preventsSilentlyDiscardingAttributes ( ) : bool

Determine if discarding guarded attribute fills is disabled.

public static

preventsAccessingMissingAttributes ( ) : bool

Determine if accessing missing attributes is disabled.

public

broadcastChannelRoute ( ) : string

Get the broadcast channel route definition that is associated with the given entity.

public

broadcastChannel ( ) : string

Get the broadcast channel name that is associated with the given entity.

public

__get ( $key ) : mixed

Dynamically retrieve attributes on the model.

Parameters

  • $key string
public

__set ( $key , $value ) : void

Dynamically set attributes on the model.

Parameters

  • $key string
  • $value mixed
public

offsetExists ( $offset ) : bool

Determine if the given attribute exists.

Parameters

  • $offset mixed
public

offsetGet ( $offset ) : mixed

Get the value for a given offset.

Parameters

  • $offset mixed
public

offsetSet ( $offset , $value ) : void

Set the value for a given offset.

Parameters

  • $offset mixed
  • $value mixed
public

offsetUnset ( $offset ) : void

Unset the value for a given offset.

Parameters

  • $offset mixed
public

__isset ( $key ) : bool

Determine if an attribute or relation exists on the model.

Parameters

  • $key string
public

__unset ( $key ) : void

Unset an attribute on the model.

Parameters

  • $key string
public

__call ( $method , $parameters ) : mixed

Handle dynamic method calls into the model.

Parameters

  • $method string
  • $parameters array
public static

__callStatic ( $method , $parameters ) : mixed

Handle dynamic static method calls into the model.

Parameters

  • $method string
  • $parameters array
public

__toString ( ) : string

Convert the model to its string representation.

public

escapeWhenCastingToString ( $escape ) : $this

Indicate that the object's string representation should be escaped when __toString is invoked.

Parameters

  • $escape bool
public

__sleep ( ) : array

Prepare the object for serialization.

public

__wakeup ( ) : void

When a model is being unserialized, check if it needs to be booted.