Regular Expressions 101

Save & Manage Regex

  • Current Version: 14
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"^\s*[\w\s]+\(.*\)\s*\K({((?>\"(?:[^\"\\]*+|\\.)*\"|'(?:[^'\\]*+|\\.)*'|//.*$|/\*[\s\S]*?\*/|#.*$|<<<\s*[\"']?(\w+)[\"']?[^;]+\3;$|[^{}<'\"/#]++|[^{}]++|(?1))*)})", flags=re.MULTILINE) test_str = ("<?php\n\n" "namespace Illuminate\\Database\\Eloquent;\n\n" "use Closure;\n" "use DateTime;\n" "use Exception;\n" "use ArrayAccess;\n" "use Carbon\\Carbon;\n" "use LogicException;\n" "use JsonSerializable;\n" "use DateTimeInterface;\n" "use Illuminate\\Support\\Arr;\n" "use Illuminate\\Support\\Str;\n" "use InvalidArgumentException;\n" "use Illuminate\\Contracts\\Support\\Jsonable;\n" "use Illuminate\\Contracts\\Events\\Dispatcher;\n" "use Illuminate\\Contracts\\Support\\Arrayable;\n" "use Illuminate\\Contracts\\Routing\\UrlRoutable;\n" "use Illuminate\\Contracts\\Queue\\QueueableEntity;\n" "use Illuminate\\Database\\Eloquent\\Relations\\Pivot;\n" "use Illuminate\\Database\\Eloquent\\Relations\\HasOne;\n" "use Illuminate\\Database\\Eloquent\\Relations\\HasMany;\n" "use Illuminate\\Database\\Eloquent\\Relations\\MorphTo;\n" "use Illuminate\\Database\\Eloquent\\Relations\\Relation;\n" "use Illuminate\\Database\\Eloquent\\Relations\\MorphOne;\n" "use Illuminate\\Support\\Collection as BaseCollection;\n" "use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;\n" "use Illuminate\\Database\\Eloquent\\Relations\\BelongsTo;\n" "use Illuminate\\Database\\Query\\Builder as QueryBuilder;\n" "use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;\n" "use Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany;\n" "use Illuminate\\Database\\Eloquent\\Relations\\HasManyThrough;\n" "use Illuminate\\Database\\ConnectionResolverInterface as Resolver;\n\n" "abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable\n" "{\n" " /**\n" " * The connection name for the model.\n" " *\n" " * @var string\n" " */\n" " protected $connection;\n\n" " /**\n" " * The table associated with the model.\n" " *\n" " * @var string\n" " */\n" " protected $table;\n\n" " /**\n" " * The primary key for the model.\n" " *\n" " * @var string\n" " */\n" " protected $primaryKey = 'id';\n\n" " /**\n" " * The \"type\" of the auto-incrementing ID.\n" " *\n" " * @var string\n" " */\n" " protected $keyType = 'int';\n\n" " /**\n" " * The number of models to return for pagination.\n" " *\n" " * @var int\n" " */\n" " protected $perPage = 15;\n\n" " /**\n" " * Indicates if the IDs are auto-incrementing.\n" " *\n" " * @var bool\n" " */\n" " public $incrementing = true;\n\n" " /**\n" " * Indicates if the model should be timestamped.\n" " *\n" " * @var bool\n" " */\n" " public $timestamps = true;\n\n" " /**\n" " * The model's attributes.\n" " *\n" " * @var array\n" " */\n" " protected $attributes = [];\n\n" " /**\n" " * The model attribute's original state.\n" " *\n" " * @var array\n" " */\n" " protected $original = [];\n\n" " /**\n" " * The loaded relationships for the model.\n" " *\n" " * @var array\n" " */\n" " protected $relations = [];\n\n" " /**\n" " * The attributes that should be hidden for arrays.\n" " *\n" " * @var array\n" " */\n" " protected $hidden = [];\n\n" " /**\n" " * The attributes that should be visible in arrays.\n" " *\n" " * @var array\n" " */\n" " protected $visible = [];\n\n" " /**\n" " * The accessors to append to the model's array form.\n" " *\n" " * @var array\n" " */\n" " protected $appends = [];\n\n" " /**\n" " * The attributes that are mass assignable.\n" " *\n" " * @var array\n" " */\n" " protected $fillable = [];\n\n" " /**\n" " * The attributes that aren't mass assignable.\n" " *\n" " * @var array\n" " */\n" " protected $guarded = ['*'];\n\n" " /**\n" " * The attributes that should be mutated to dates.\n" " *\n" " * @var array\n" " */\n" " protected $dates = [];\n\n" " /**\n" " * The storage format of the model's date columns.\n" " *\n" " * @var string\n" " */\n" " protected $dateFormat;\n\n" " /**\n" " * The attributes that should be cast to native types.\n" " *\n" " * @var array\n" " */\n" " protected $casts = [];\n\n" " /**\n" " * The relationships that should be touched on save.\n" " *\n" " * @var array\n" " */\n" " protected $touches = [];\n\n" " /**\n" " * User exposed observable events.\n" " *\n" " * @var array\n" " */\n" " protected $observables = [];\n\n" " /**\n" " * The relations to eager load on every query.\n" " *\n" " * @var array\n" " */\n" " protected $with = [];\n\n" " /**\n" " * The class name to be used in polymorphic relations.\n" " *\n" " * @var string\n" " */\n" " protected $morphClass;\n\n" " /**\n" " * Indicates if the model exists.\n" " *\n" " * @var bool\n" " */\n" " public $exists = false;\n\n" " /**\n" " * Indicates if the model was inserted during the current request lifecycle.\n" " *\n" " * @var bool\n" " */\n" " public $wasRecentlyCreated = false;\n\n" " /**\n" " * Indicates whether attributes are snake cased on arrays.\n" " *\n" " * @var bool\n" " */\n" " public static $snakeAttributes = true;\n\n" " /**\n" " * The connection resolver instance.\n" " *\n" " * @var \\Illuminate\\Database\\ConnectionResolverInterface\n" " */\n" " protected static $resolver;\n\n" " /**\n" " * The event dispatcher instance.\n" " *\n" " * @var \\Illuminate\\Contracts\\Events\\Dispatcher\n" " */\n" " protected static $dispatcher;\n\n" " /**\n" " * The array of booted models.\n" " *\n" " * @var array\n" " */\n" " protected static $booted = [];\n\n" " /**\n" " * The array of global scopes on the model.\n" " *\n" " * @var array\n" " */\n" " protected static $globalScopes = [];\n\n" " /**\n" " * Indicates if all mass assignment is enabled.\n" " *\n" " * @var bool\n" " */\n" " protected static $unguarded = false;\n\n" " /**\n" " * The cache of the mutated attributes for each class.\n" " *\n" " * @var array\n" " */\n" " protected static $mutatorCache = [];\n\n" " /**\n" " * The many to many relationship methods.\n" " *\n" " * @var array\n" " */\n" " public static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];\n\n" " /**\n" " * The name of the \"created at\" column.\n" " *\n" " * @var string\n" " */\n" " const CREATED_AT = 'created_at';\n\n" " /**\n" " * The name of the \"updated at\" column.\n" " *\n" " * @var string\n" " */\n" " const UPDATED_AT = 'updated_at';\n\n" " /**\n" " * Create a new Eloquent model instance.\n" " *\n" " * @param array $attributes\n" " * @return void\n" " */\n" " public function __construct(array $attributes = [])\n" " {\n" " $str = \"\\\"\";\n" " $this->bootIfNotBooted();\n\n" " $this->syncOriginal();\n\n" " $this->fill($attributes);\n" " }\n\n" " /**\n" " * Check if the model needs to be booted and if so, do it.\n" " *\n" " * @return void\n" " */\n" " protected function bootIfNotBooted()\n" " {\n" " if (! isset(static::$booted[static::class])) {\n" " static::$booted[static::class] = true;\n\n" " $this->fireModelEvent('booting', false);\n\n" " static::boot();\n\n" " $this->fireModelEvent('booted', false);\n" " }\n" " }\n\n" " /**\n" " * The \"booting\" method of the model.\n" " *\n" " * @return void\n" " */\n" " protected static function boot()\n" " {\n" " static::bootTraits();\n" " }\n\n" " /**\n" " * Boot all of the bootable traits on the model.\n" " *\n" " * @return void\n" " */\n" " protected static function bootTraits()\n" " {\n" " $class = static::class;\n\n" " foreach (class_uses_recursive($class) as $trait) {\n" " if (method_exists($class, $method = 'boot'.class_basename($trait))) {\n" " forward_static_call([$class, $method]);\n" " }\n" " }\n" " }\n\n" " /**\n" " * Clear the list of booted models so they will be re-booted.\n" " *\n" " * @return void\n" " */\n" " public static function clearBootedModels()\n" " {\n" " static::$booted = [];\n" " static::$globalScopes = [];\n" " }\n\n" " /**\n" " * Register a new global scope on the model.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Scope|\\Closure|string $scope\n" " * @param \\Closure|null $implementation\n" " * @return mixed\n" " *\n" " * @throws \\InvalidArgumentException\n" " */\n" " public static function addGlobalScope($scope, Closure $implementation = null)\n" " {\n" " if (is_string($scope) && $implementation !== null) {\n" " return static::$globalScopes[static::class][$scope] = $implementation;\n" " }\n\n" " if ($scope instanceof Closure) {\n" " return static::$globalScopes[static::class][spl_object_hash($scope)] = $scope;\n" " }\n\n" " if ($scope instanceof Scope) {\n" " return static::$globalScopes[static::class][get_class($scope)] = $scope;\n" " }\n\n" " throw new InvalidArgumentException('Global scope must be an instance of Closure or Scope.');\n" " }\n\n" " /**\n" " * Determine if a model has a global scope.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Scope|string $scope\n" " * @return bool\n" " */\n" " public static function hasGlobalScope($scope)\n" " {\n" " return ! is_null(static::getGlobalScope($scope));\n" " }\n\n" " /**\n" " * Get a global scope registered with the model.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Scope|string $scope\n" " * @return \\Illuminate\\Database\\Eloquent\\Scope|\\Closure|null\n" " */\n" " public static function getGlobalScope($scope)\n" " {\n" " if (! is_string($scope)) {\n" " $scope = get_class($scope);\n" " }\n\n" " return Arr::get(static::$globalScopes, static::class.'.'.$scope);\n" " }\n\n" " /**\n" " * Get the global scopes for this class instance.\n" " *\n" " * @return array\n" " */\n" " public function getGlobalScopes()\n" " {\n" " return Arr::get(static::$globalScopes, static::class, []);\n" " }\n\n" " /**\n" " * Register an observer with the Model.\n" " *\n" " * @param object|string $class\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function observe($class, $priority = 0)\n" " {\n" " $instance = new static;\n\n" " $className = is_string($class) ? $class : get_class($class);\n\n" " // When registering a model observer, we will spin through the possible events\n" " // and determine if this observer has that method. If it does, we will hook\n" " // it into the model's event system, making it convenient to watch these.\n" " foreach ($instance->getObservableEvents() as $event) {\n" " if (method_exists($class, $event)) {\n" " static::registerModelEvent($event, $className.'@'.$event, $priority);\n" " }\n" " }\n" " }\n\n" " /**\n" " * Fill the model with an array of attributes.\n" " *\n" " * @param array $attributes\n" " * @return $this\n" " *\n" " * @throws \\Illuminate\\Database\\Eloquent\\MassAssignmentException\n" " */\n" " public function fill(array $attributes)\n" " {\n" " $totallyGuarded = $this->totallyGuarded();\n\n" " foreach ($this->fillableFromArray($attributes) as $key => $value) {\n" " $key = $this->removeTableFromKey($key);\n\n" " // The developers may choose to place some attributes in the \"fillable\"\n" " // array, which means only those attributes may be set through mass\n" " // assignment to the model, and all others will just be ignored.\n" " if ($this->isFillable($key)) {\n" " $this->setAttribute($key, $value);\n" " } elseif ($totallyGuarded) {\n" " throw new MassAssignmentException($key);\n" " }\n" " }\n\n" " return $this;\n" " }\n\n" " /**\n" " * Fill the model with an array of attributes. Force mass assignment.\n" " *\n" " * @param array $attributes\n" " * @return $this\n" " */\n" " public function forceFill(array $attributes)\n" " {\n" " return static::unguarded(function () use ($attributes) {\n" " return $this->fill($attributes);\n" " });\n" " }\n\n" " /**\n" " * Get the fillable attributes of a given array.\n" " *\n" " * @param array $attributes\n" " * @return array\n" " */\n" " protected function fillableFromArray(array $attributes)\n" " {\n" " if (count($this->getFillable()) > 0 && ! static::$unguarded) {\n" " return array_intersect_key($attributes, array_flip($this->getFillable()));\n" " }\n\n" " return $attributes;\n" " }\n\n" " /**\n" " * Create a new instance of the given model.\n" " *\n" " * @param array $attributes\n" " * @param bool $exists\n" " * @return static\n" " */\n" " public function newInstance($attributes = [], $exists = false)\n" " {\n" " // This method just provides a convenient way for us to generate fresh model\n" " // instances of this current model. It is particularly useful during the\n" " // hydration of new objects via the Eloquent query builder instances.\n" " $model = new static((array) $attributes);\n\n" " $model->exists = $exists;\n\n" " return $model;\n" " }\n\n" " /**\n" " * Create a new model instance that is existing.\n" " *\n" " * @param array $attributes\n" " * @param string|null $connection\n" " * @return static\n" " */\n" " public function newFromBuilder($attributes = [], $connection = null)\n" " {\n" " $model = $this->newInstance([], true);\n\n" " $model->setRawAttributes((array) $attributes, true);\n\n" " $model->setConnection($connection ?: $this->connection);\n\n" " return $model;\n" " }\n\n" " /**\n" " * Create a collection of models from plain arrays.\n" " *\n" " * @param array $items\n" " * @param string|null $connection\n" " * @return \\Illuminate\\Database\\Eloquent\\Collection\n" " */\n" " public static function hydrate(array $items, $connection = null)\n" " {\n" " $instance = (new static)->setConnection($connection);\n\n" " $items = array_map(function ($item) use ($instance) {\n" " return $instance->newFromBuilder($item);\n" " }, $items);\n\n" " return $instance->newCollection($items);\n" " }\n\n" " /**\n" " * Create a collection of models from a raw query.\n" " *\n" " * @param string $query\n" " * @param array $bindings\n" " * @param string|null $connection\n" " * @return \\Illuminate\\Database\\Eloquent\\Collection\n" " */\n" " public static function hydrateRaw($query, $bindings = [], $connection = null)\n" " {\n" " $instance = (new static)->setConnection($connection);\n\n" " $items = $instance->getConnection()->select($query, $bindings);\n\n" " return static::hydrate($items, $connection);\n" " }\n\n" " /**\n" " * Save a new model and return the instance.\n" " *\n" " * @param array $attributes\n" " * @return static\n" " */\n" " public static function create(array $attributes = [])\n" " {\n" " $model = new static($attributes);\n\n" " $model->save();\n\n" " return $model;\n" " }\n\n" " /**\n" " * Save a new model and return the instance. Allow mass-assignment.\n" " *\n" " * @param array $attributes\n" " * @return static\n" " */\n" " public static function forceCreate(array $attributes)\n" " {\n" " return static::unguarded(function () use ($attributes) {\n" " return (new static)->create($attributes);\n" " });\n" " }\n\n" " /**\n" " * Begin querying the model.\n" " *\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder\n" " */\n" " public static function query()\n" " {\n" " return (new static)->newQuery();\n" " }\n\n" " /**\n" " * Begin querying the model on a given connection.\n" " *\n" " * @param string|null $connection\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder\n" " */\n" " public static function on($connection = null)\n" " {\n" " // First we will just create a fresh instance of this model, and then we can\n" " // set the connection on the model so that it is be used for the queries\n" " // we execute, as well as being set on each relationship we retrieve.\n" " $instance = new static;\n\n" " $instance->setConnection($connection);\n\n" " return $instance->newQuery();\n" " }\n\n" " /**\n" " * Begin querying the model on the write connection.\n" " *\n" " * @return \\Illuminate\\Database\\Query\\Builder\n" " */\n" " public static function onWriteConnection()\n" " {\n" " $instance = new static;\n\n" " return $instance->newQuery()->useWritePdo();\n" " }\n\n" " /**\n" " * Get all of the models from the database.\n" " *\n" " * @param array|mixed $columns\n" " * @return \\Illuminate\\Database\\Eloquent\\Collection|static[]\n" " */\n" " public static function all($columns = ['*'])\n" " {\n" " $columns = is_array($columns) ? $columns : func_get_args();\n\n" " $instance = new static;\n\n" " return $instance->newQuery()->get($columns);\n" " }\n\n" " /**\n" " * Reload a fresh model instance from the database.\n" " *\n" " * @param array|string $with\n" " * @return $this|null\n" " */\n" " public function fresh($with = [])\n" " {\n" " if (! $this->exists) {\n" " return;\n" " }\n\n" " if (is_string($with)) {\n" " $with = func_get_args();\n" " }\n\n" " $key = $this->getKeyName();\n\n" " return static::with($with)->where($key, $this->getKey())->first();\n" " }\n\n" " /**\n" " * Eager load relations on the model.\n" " *\n" " * @param array|string $relations\n" " * @return $this\n" " */\n" " public function load($relations)\n" " {\n" " if (is_string($relations)) {\n" " $relations = func_get_args();\n" " }\n\n" " $query = $this->newQuery()->with($relations);\n\n" " $query->eagerLoadRelations([$this]);\n\n" " return $this;\n" " }\n\n" " /**\n" " * Begin querying a model with eager loading.\n" " *\n" " * @param array|string $relations\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder|static\n" " */\n" " public static function with($relations)\n" " {\n" " if (is_string($relations)) {\n" " $relations = func_get_args();\n" " }\n\n" " $instance = new static;\n\n" " return $instance->newQuery()->with($relations);\n" " }\n\n" " /**\n" " * Append attributes to query when building a query.\n" " *\n" " * @param array|string $attributes\n" " * @return $this\n" " */\n" " public function append($attributes)\n" " {\n" " if (is_string($attributes)) {\n" " $attributes = func_get_args();\n" " }\n\n" " $this->appends = array_unique(\n" " array_merge($this->appends, $attributes)\n" " );\n\n" " return $this;\n" " }\n\n" " /**\n" " * Define a one-to-one relationship.\n" " *\n" " * @param string $related\n" " * @param string $foreignKey\n" " * @param string $localKey\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\HasOne\n" " */\n" " public function hasOne($related, $foreignKey = null, $localKey = null)\n" " {\n" " $foreignKey = $foreignKey ?: $this->getForeignKey();\n\n" " $instance = new $related;\n\n" " $localKey = $localKey ?: $this->getKeyName();\n\n" " return new HasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);\n" " }\n\n" " /**\n" " * Define a polymorphic one-to-one relationship.\n" " *\n" " * @param string $related\n" " * @param string $name\n" " * @param string $type\n" " * @param string $id\n" " * @param string $localKey\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\MorphOne\n" " */\n" " public function morphOne($related, $name, $type = null, $id = null, $localKey = null)\n" " {\n" " $instance = new $related;\n\n" " list($type, $id) = $this->getMorphs($name, $type, $id);\n\n" " $table = $instance->getTable();\n\n" " $localKey = $localKey ?: $this->getKeyName();\n\n" " return new MorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);\n" " }\n\n" " /**\n" " * Define an inverse one-to-one or many relationship.\n" " *\n" " * @param string $related\n" " * @param string $foreignKey\n" " * @param string $otherKey\n" " * @param string $relation\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo\n" " */\n" " public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)\n" " {\n" " // If no relation name was given, we will use this debug backtrace to extract\n" " // the calling method's name and use that as the relationship name as most\n" " // of the time this will be what we desire to use for the relationships.\n" " if (is_null($relation)) {\n" " list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);\n\n" " $relation = $caller['function'];\n" " }\n\n" " // If no foreign key was supplied, we can use a backtrace to guess the proper\n" " // foreign key name by using the name of the relationship function, which\n" " // when combined with an \"_id\" should conventionally match the columns.\n" " if (is_null($foreignKey)) {\n" " $foreignKey = Str::snake($relation).'_id';\n" " }\n\n" " $instance = new $related;\n\n" " // Once we have the foreign key names, we'll just create a new Eloquent query\n" " // for the related models and returns the relationship instance which will\n" " // actually be responsible for retrieving and hydrating every relations.\n" " $query = $instance->newQuery();\n\n" " $otherKey = $otherKey ?: $instance->getKeyName();\n\n" " return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);\n" " }\n\n" " /**\n" " * Define a polymorphic, inverse one-to-one or many relationship.\n" " *\n" " * @param string $name\n" " * @param string $type\n" " * @param string $id\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\MorphTo\n" " */\n" " public function morphTo($name = null, $type = null, $id = null)\n" " {\n" " // If no name is provided, we will use the backtrace to get the function name\n" " // since that is most likely the name of the polymorphic interface. We can\n" " // use that to get both the class and foreign key that will be utilized.\n" " if (is_null($name)) {\n" " list($current, $caller) = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);\n\n" " $name = Str::snake($caller['function']);\n" " }\n\n" " list($type, $id) = $this->getMorphs($name, $type, $id);\n\n" " // If the type value is null it is probably safe to assume we're eager loading\n" " // the relationship. When that is the case we will pass in a dummy query as\n" " // there are multiple types in the morph and we can't use single queries.\n" " if (empty($class = $this->$type)) {\n" " return new MorphTo(\n" " $this->newQuery(), $this, $id, null, $type, $name\n" " );\n" " }\n\n" " // If we are not eager loading the relationship we will essentially treat this\n" " // as a belongs-to style relationship since morph-to extends that class and\n" " // we will pass in the appropriate values so that it behaves as expected.\n" " else {\n" " $class = $this->getActualClassNameForMorph($class);\n\n" " $instance = new $class;\n\n" " return new MorphTo(\n" " $instance->newQuery(), $this, $id, $instance->getKeyName(), $type, $name\n" " );\n" " }\n" " }\n\n" " /**\n" " * Retrieve the fully qualified class name from a slug.\n" " *\n" " * @param string $class\n" " * @return string\n" " */\n" " public function getActualClassNameForMorph($class)\n" " {\n" " return Arr::get(Relation::morphMap(), $class, $class);\n" " }\n\n" " /**\n" " * Define a one-to-many relationship.\n" " *\n" " * @param string $related\n" " * @param string $foreignKey\n" " * @param string $localKey\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\HasMany\n" " */\n" " public function hasMany($related, $foreignKey = null, $localKey = null)\n" " {\n" " $foreignKey = $foreignKey ?: $this->getForeignKey();\n\n" " $instance = new $related;\n\n" " $localKey = $localKey ?: $this->getKeyName();\n\n" " return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);\n" " }\n\n" " /**\n" " * Define a has-many-through relationship.\n" " *\n" " * @param string $related\n" " * @param string $through\n" " * @param string|null $firstKey\n" " * @param string|null $secondKey\n" " * @param string|null $localKey\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\HasManyThrough\n" " */\n" " public function hasManyThrough($related, $through, $firstKey = null, $secondKey = null, $localKey = null)\n" " {\n" " $through = new $through;\n\n" " $firstKey = $firstKey ?: $this->getForeignKey();\n\n" " $secondKey = $secondKey ?: $through->getForeignKey();\n\n" " $localKey = $localKey ?: $this->getKeyName();\n\n" " return new HasManyThrough((new $related)->newQuery(), $this, $through, $firstKey, $secondKey, $localKey);\n" " }\n\n" " /**\n" " * Define a polymorphic one-to-many relationship.\n" " *\n" " * @param string $related\n" " * @param string $name\n" " * @param string $type\n" " * @param string $id\n" " * @param string $localKey\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\MorphMany\n" " */\n" " public function morphMany($related, $name, $type = null, $id = null, $localKey = null)\n" " {\n" " $instance = new $related;\n\n" " // Here we will gather up the morph type and ID for the relationship so that we\n" " // can properly query the intermediate table of a relation. Finally, we will\n" " // get the table and create the relationship instances for the developers.\n" " list($type, $id) = $this->getMorphs($name, $type, $id);\n\n" " $table = $instance->getTable();\n\n" " $localKey = $localKey ?: $this->getKeyName();\n\n" " return new MorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);\n" " }\n\n" " /**\n" " * Define a many-to-many relationship.\n" " *\n" " * @param string $related\n" " * @param string $table\n" " * @param string $foreignKey\n" " * @param string $otherKey\n" " * @param string $relation\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany\n" " */\n" " public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)\n" " {\n" " // If no relationship name was passed, we will pull backtraces to get the\n" " // name of the calling function. We will use that function name as the\n" " // title of this relation since that is a great convention to apply.\n" " if (is_null($relation)) {\n" " $relation = $this->getBelongsToManyCaller();\n" " }\n\n" " // First, we'll need to determine the foreign key and \"other key\" for the\n" " // relationship. Once we have determined the keys we'll make the query\n" " // instances as well as the relationship instances we need for this.\n" " $foreignKey = $foreignKey ?: $this->getForeignKey();\n\n" " $instance = new $related;\n\n" " $otherKey = $otherKey ?: $instance->getForeignKey();\n\n" " // If no table name was provided, we can guess it by concatenating the two\n" " // models using underscores in alphabetical order. The two model names\n" " // are transformed to snake case from their default CamelCase also.\n" " if (is_null($table)) {\n" " $table = $this->joiningTable($related);\n" " }\n\n" " // Now we're ready to create a new query builder for the related model and\n" " // the relationship instances for the relation. The relations will set\n" " // appropriate query constraint and entirely manages the hydrations.\n" " $query = $instance->newQuery();\n\n" " return new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation);\n" " }\n\n" " /**\n" " * Define a polymorphic many-to-many relationship.\n" " *\n" " * @param string $related\n" " * @param string $name\n" " * @param string $table\n" " * @param string $foreignKey\n" " * @param string $otherKey\n" " * @param bool $inverse\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\MorphToMany\n" " */\n" " public function morphToMany($related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)\n" " {\n" " $caller = $this->getBelongsToManyCaller();\n\n" " // First, we will need to determine the foreign key and \"other key\" for the\n" " // relationship. Once we have determined the keys we will make the query\n" " // instances, as well as the relationship instances we need for these.\n" " $foreignKey = $foreignKey ?: $name.'_id';\n\n" " $instance = new $related;\n\n" " $otherKey = $otherKey ?: $instance->getForeignKey();\n\n" " // Now we're ready to create a new query builder for this related model and\n" " // the relationship instances for this relation. This relations will set\n" " // appropriate query constraints then entirely manages the hydrations.\n" " $query = $instance->newQuery();\n\n" " $table = $table ?: Str::plural($name);\n\n" " return new MorphToMany(\n" " $query, $this, $name, $table, $foreignKey,\n" " $otherKey, $caller, $inverse\n" " );\n" " }\n\n" " /**\n" " * Define a polymorphic, inverse many-to-many relationship.\n" " *\n" " * @param string $related\n" " * @param string $name\n" " * @param string $table\n" " * @param string $foreignKey\n" " * @param string $otherKey\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\MorphToMany\n" " */\n" " public function morphedByMany($related, $name, $table = null, $foreignKey = null, $otherKey = null)\n" " {\n" " $foreignKey = $foreignKey ?: $this->getForeignKey();\n\n" " // For the inverse of the polymorphic many-to-many relations, we will change\n" " // the way we determine the foreign and other keys, as it is the opposite\n" " // of the morph-to-many method since we're figuring out these inverses.\n" " $otherKey = $otherKey ?: $name.'_id';\n\n" " return $this->morphToMany($related, $name, $table, $foreignKey, $otherKey, true);\n" " }\n\n" " /**\n" " * Get the relationship name of the belongs to many.\n" " *\n" " * @return string\n" " */\n" " protected function getBelongsToManyCaller()\n" " {\n" " $self = __FUNCTION__;\n\n" " $caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($trace) use ($self) {\n" " $caller = $trace['function'];\n\n" " return ! in_array($caller, Model::$manyMethods) && $caller != $self;\n" " });\n\n" " return ! is_null($caller) ? $caller['function'] : null;\n" " }\n\n" " /**\n" " * Get the joining table name for a many-to-many relation.\n" " *\n" " * @param string $related\n" " * @return string\n" " */\n" " public function joiningTable($related)\n" " {\n" " // The joining table name, by convention, is simply the snake cased models\n" " // sorted alphabetically and concatenated with an underscore, so we can\n" " // just sort the models and join them together to get the table name.\n" " $base = Str::snake(class_basename($this));\n\n" " $related = Str::snake(class_basename($related));\n\n" " $models = [$related, $base];\n\n" " // Now that we have the model names in an array we can just sort them and\n" " // use the implode function to join them together with an underscores,\n" " // which is typically used by convention within the database system.\n" " sort($models);\n\n" " return strtolower(implode('_', $models));\n" " }\n\n" " /**\n" " * Destroy the models for the given IDs.\n" " *\n" " * @param array|int $ids\n" " * @return int\n" " */\n" " public static function destroy($ids)\n" " {\n" " // We'll initialize a count here so we will return the total number of deletes\n" " // for the operation. The developers can then check this number as a boolean\n" " // type value or get this total count of records deleted for logging, etc.\n" " $count = 0;\n\n" " $ids = is_array($ids) ? $ids : func_get_args();\n\n" " $instance = new static;\n\n" " // We will actually pull the models from the database table and call delete on\n" " // each of them individually so that their events get fired properly with a\n" " // correct set of attributes in case the developers wants to check these.\n" " $key = $instance->getKeyName();\n\n" " foreach ($instance->whereIn($key, $ids)->get() as $model) {\n" " if ($model->delete()) {\n" " $count++;\n" " }\n" " }\n\n" " return $count;\n" " }\n\n" " /**\n" " * Delete the model from the database.\n" " *\n" " * @return bool|null\n" " *\n" " * @throws \\Exception\n" " */\n" " public function delete()\n" " {\n" " if (is_null($this->getKeyName())) {\n" " throw new Exception('No primary key defined on model.');\n" " }\n\n" " if ($this->exists) {\n" " if ($this->fireModelEvent('deleting') === false) {\n" " return false;\n" " }\n\n" " // Here, we'll touch the owning models, verifying these timestamps get updated\n" " // for the models. This will allow any caching to get broken on the parents\n" " // by the timestamp. Then we will go ahead and delete the model instance.\n" " $this->touchOwners();\n\n" " $this->performDeleteOnModel();\n\n" " $this->exists = false;\n\n" " // Once the model has been deleted, we will fire off the deleted event so that\n" " // the developers may hook into post-delete operations. We will then return\n" " // a boolean true as the delete is presumably successful on the database.\n" " $this->fireModelEvent('deleted', false);\n\n" " return true;\n" " }\n" " }\n\n" " /**\n" " * Force a hard delete on a soft deleted model.\n" " *\n" " * This method protects developers from running forceDelete when trait is missing.\n" " *\n" " * @return bool|null\n" " */\n" " public function forceDelete()\n" " {\n" " return $this->delete();\n" " }\n\n" " /**\n" " * Perform the actual delete query on this model instance.\n" " *\n" " * @return void\n" " */\n" " protected function performDeleteOnModel()\n" " {\n" " $this->setKeysForSaveQuery($this->newQueryWithoutScopes())->delete();\n" " }\n\n" " /**\n" " * Register a saving model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function saving($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('saving', $callback, $priority);\n" " }\n\n" " /**\n" " * Register a saved model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function saved($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('saved', $callback, $priority);\n" " }\n\n" " /**\n" " * Register an updating model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function updating($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('updating', $callback, $priority);\n" " }\n\n" " /**\n" " * Register an updated model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function updated($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('updated', $callback, $priority);\n" " }\n\n" " /**\n" " * Register a creating model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function creating($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('creating', $callback, $priority);\n" " }\n\n" " /**\n" " * Register a created model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function created($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('created', $callback, $priority);\n" " }\n\n" " /**\n" " * Register a deleting model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function deleting($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('deleting', $callback, $priority);\n" " }\n\n" " /**\n" " * Register a deleted model event with the dispatcher.\n" " *\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " public static function deleted($callback, $priority = 0)\n" " {\n" " static::registerModelEvent('deleted', $callback, $priority);\n" " }\n\n" " /**\n" " * Remove all of the event listeners for the model.\n" " *\n" " * @return void\n" " */\n" " public static function flushEventListeners()\n" " {\n" " if (! isset(static::$dispatcher)) {\n" " return;\n" " }\n\n" " $instance = new static;\n\n" " foreach ($instance->getObservableEvents() as $event) {\n" " static::$dispatcher->forget(\"eloquent.{$event}: \".static::class);\n" " }\n" " }\n\n" " /**\n" " * Register a model event with the dispatcher.\n" " *\n" " * @param string $event\n" " * @param \\Closure|string $callback\n" " * @param int $priority\n" " * @return void\n" " */\n" " protected static function registerModelEvent($event, $callback, $priority = 0)\n" " {\n" " if (isset(static::$dispatcher)) {\n" " $name = static::class;\n\n" " static::$dispatcher->listen(\"eloquent.{$event}: {$name}\", $callback, $priority);\n" " }\n" " }\n\n" " /**\n" " * Get the observable event names.\n" " *\n" " * @return array\n" " */\n" " public function getObservableEvents()\n" " {\n" " return array_merge(\n" " [\n" " 'creating', 'created', 'updating', 'updated',\n" " 'deleting', 'deleted', 'saving', 'saved',\n" " 'restoring', 'restored',\n" " ],\n" " $this->observables\n" " );\n" " }\n\n" " /**\n" " * Set the observable event names.\n" " *\n" " * @param array $observables\n" " * @return $this\n" " */\n" " public function setObservableEvents(array $observables)\n" " {\n" " $this->observables = $observables;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Add an observable event name.\n" " *\n" " * @param array|mixed $observables\n" " * @return void\n" " */\n" " public function addObservableEvents($observables)\n" " {\n" " $observables = is_array($observables) ? $observables : func_get_args();\n\n" " $this->observables = array_unique(array_merge($this->observables, $observables));\n" " }\n\n" " /**\n" " * Remove an observable event name.\n" " *\n" " * @param array|mixed $observables\n" " * @return void\n" " */\n" " public function removeObservableEvents($observables)\n" " {\n" " $observables = is_array($observables) ? $observables : func_get_args();\n\n" " $this->observables = array_diff($this->observables, $observables);\n" " }\n\n" " /**\n" " * Increment a column's value by a given amount.\n" " *\n" " * @param string $column\n" " * @param int $amount\n" " * @param array $extra\n" " * @return int\n" " */\n" " protected function increment($column, $amount = 1, array $extra = [])\n" " {\n" " return $this->incrementOrDecrement($column, $amount, $extra, 'increment');\n" " }\n\n" " /**\n" " * Decrement a column's value by a given amount.\n" " *\n" " * @param string $column\n" " * @param int $amount\n" " * @param array $extra\n" " * @return int\n" " */\n" " protected function decrement($column, $amount = 1, array $extra = [])\n" " {\n" " return $this->incrementOrDecrement($column, $amount, $extra, 'decrement');\n" " }\n\n" " /**\n" " * Run the increment or decrement method on the model.\n" " *\n" " * @param string $column\n" " * @param int $amount\n" " * @param array $extra\n" " * @param string $method\n" " * @return int\n" " */\n" " protected function incrementOrDecrement($column, $amount, $extra, $method)\n" " {\n" " $query = $this->newQuery();\n\n" " if (! $this->exists) {\n" " return $query->{$method}($column, $amount, $extra);\n" " }\n\n" " $this->incrementOrDecrementAttributeValue($column, $amount, $method);\n\n" " return $query->where($this->getKeyName(), $this->getKey())->{$method}($column, $amount, $extra);\n" " }\n\n" " /**\n" " * Increment the underlying attribute value and sync with original.\n" " *\n" " * @param string $column\n" " * @param int $amount\n" " * @param string $method\n" " * @return void\n" " */\n" " protected function incrementOrDecrementAttributeValue($column, $amount, $method)\n" " {\n" " $this->{$column} = $this->{$column} + ($method == 'increment' ? $amount : $amount * -1);\n\n" " $this->syncOriginalAttribute($column);\n" " }\n\n" " /**\n" " * Update the model in the database.\n" " *\n" " * @param array $attributes\n" " * @param array $options\n" " * @return bool|int\n" " */\n" " public function update(array $attributes = [], array $options = [])\n" " {\n" " if (! $this->exists) {\n" " return false;\n" " }\n\n" " return $this->fill($attributes)->save($options);\n" " }\n\n" " /**\n" " * Save the model and all of its relationships.\n" " *\n" " * @return bool\n" " */\n" " public function push()\n" " {\n" " if (! $this->save()) {\n" " return false;\n" " }\n\n" " // To sync all of the relationships to the database, we will simply spin through\n" " // the relationships and save each model via this \"push\" method, which allows\n" " // us to recurse into all of these nested relations for the model instance.\n" " foreach ($this->relations as $models) {\n" " $models = $models instanceof Collection\n" " ? $models->all() : [$models];\n\n" " foreach (array_filter($models) as $model) {\n" " if (! $model->push()) {\n" " return false;\n" " }\n" " }\n" " }\n\n" " return true;\n" " }\n\n" " /**\n" " * Save the model to the database.\n" " *\n" " * @param array $options\n" " * @return bool\n" " */\n" " public function save(array $options = [])\n" " {\n" " $query = $this->newQueryWithoutScopes();\n\n" " // If the \"saving\" event returns false we'll bail out of the save and return\n" " // false, indicating that the save failed. This provides a chance for any\n" " // listeners to cancel save operations if validations fail or whatever.\n" " if ($this->fireModelEvent('saving') === false) {\n" " return false;\n" " }\n\n" " // If the model already exists in the database we can just update our record\n" " // that is already in this database using the current IDs in this \"where\"\n" " // clause to only update this model. Otherwise, we'll just insert them.\n" " if ($this->exists) {\n" " $saved = $this->performUpdate($query);\n" " }\n\n" " // If the model is brand new, we'll insert it into our database and set the\n" " // ID attribute on the model to the value of the newly inserted row's ID\n" " // which is typically an auto-increment value managed by the database.\n" " else {\n" " $saved = $this->performInsert($query);\n" " }\n\n" " if ($saved) {\n" " $this->finishSave($options);\n" " }\n\n" " return $saved;\n" " }\n\n" " /**\n" " * Save the model to the database using transaction.\n" " *\n" " * @param array $options\n" " * @return bool\n" " *\n" " * @throws \\Throwable\n" " */\n" " public function saveOrFail(array $options = [])\n" " {\n" " return $this->getConnection()->transaction(function () use ($options) {\n" " return $this->save($options);\n" " });\n" " }\n\n" " /**\n" " * Finish processing on a successful save operation.\n" " *\n" " * @param array $options\n" " * @return void\n" " */\n" " protected function finishSave(array $options)\n" " {\n" " $this->fireModelEvent('saved', false);\n\n" " $this->syncOriginal();\n\n" " if (Arr::get($options, 'touch', true)) {\n" " $this->touchOwners();\n" " }\n" " }\n\n" " /**\n" " * Perform a model update operation.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Builder $query\n" " * @return bool\n" " */\n" " protected function performUpdate(Builder $query)\n" " {\n" " $dirty = $this->getDirty();\n\n" " if (count($dirty) > 0) {\n" " // If the updating event returns false, we will cancel the update operation so\n" " // developers can hook Validation systems into their models and cancel this\n" " // operation if the model does not pass validation. Otherwise, we update.\n" " if ($this->fireModelEvent('updating') === false) {\n" " return false;\n" " }\n\n" " // First we need to create a fresh query instance and touch the creation and\n" " // update timestamp on the model which are maintained by us for developer\n" " // convenience. Then we will just continue saving the model instances.\n" " if ($this->timestamps) {\n" " $this->updateTimestamps();\n" " }\n\n" " // Once we have run the update operation, we will fire the \"updated\" event for\n" " // this model instance. This will allow developers to hook into these after\n" " // models are updated, giving them a chance to do any special processing.\n" " $dirty = $this->getDirty();\n\n" " if (count($dirty) > 0) {\n" " $numRows = $this->setKeysForSaveQuery($query)->update($dirty);\n\n" " $this->fireModelEvent('updated', false);\n" " }\n" " }\n\n" " return true;\n" " }\n\n" " /**\n" " * Perform a model insert operation.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Builder $query\n" " * @return bool\n" " */\n" " protected function performInsert(Builder $query)\n" " {\n" " if ($this->fireModelEvent('creating') === false) {\n" " return false;\n" " }\n\n" " // First we'll need to create a fresh query instance and touch the creation and\n" " // update timestamps on this model, which are maintained by us for developer\n" " // convenience. After, we will just continue saving these model instances.\n" " if ($this->timestamps) {\n" " $this->updateTimestamps();\n" " }\n\n" " // If the model has an incrementing key, we can use the \"insertGetId\" method on\n" " // the query builder, which will give us back the final inserted ID for this\n" " // table from the database. Not all tables have to be incrementing though.\n" " $attributes = $this->attributes;\n\n" " if ($this->getIncrementing()) {\n" " $this->insertAndSetId($query, $attributes);\n" " }\n\n" " // If the table isn't incrementing we'll simply insert these attributes as they\n" " // are. These attribute arrays must contain an \"id\" column previously placed\n" " // there by the developer as the manually determined key for these models.\n" " else {\n" " $query->insert($attributes);\n" " }\n\n" " // We will go ahead and set the exists property to true, so that it is set when\n" " // the created event is fired, just in case the developer tries to update it\n" " // during the event. This will allow them to do so and run an update here.\n" " $this->exists = true;\n\n" " $this->wasRecentlyCreated = true;\n\n" " $this->fireModelEvent('created', false);\n\n" " return true;\n" " }\n\n" " /**\n" " * Insert the given attributes and set the ID on the model.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Builder $query\n" " * @param array $attributes\n" " * @return void\n" " */\n" " protected function insertAndSetId(Builder $query, $attributes)\n" " {\n" " $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());\n\n" " $this->setAttribute($keyName, $id);\n" " }\n\n" " /**\n" " * Touch the owning relations of the model.\n" " *\n" " * @return void\n" " */\n" " public function touchOwners()\n" " {\n" " foreach ($this->touches as $relation) {\n" " $this->$relation()->touch();\n\n" " if ($this->$relation instanceof self) {\n" " $this->$relation->fireModelEvent('saved', false);\n\n" " $this->$relation->touchOwners();\n" " } elseif ($this->$relation instanceof Collection) {\n" " $this->$relation->each(function (Model $relation) {\n" " $relation->touchOwners();\n" " });\n" " }\n" " }\n" " }\n\n" " /**\n" " * Determine if the model touches a given relation.\n" " *\n" " * @param string $relation\n" " * @return bool\n" " */\n" " public function touches($relation)\n" " {\n" " return in_array($relation, $this->touches);\n" " }\n\n" " /**\n" " * Fire the given event for the model.\n" " *\n" " * @param string $event\n" " * @param bool $halt\n" " * @return mixed\n" " */\n" " protected function fireModelEvent($event, $halt = true)\n" " {\n" " if (! isset(static::$dispatcher)) {\n" " return true;\n" " }\n\n" " // We will append the names of the class to the event to distinguish it from\n" " // other model events that are fired, allowing us to listen on each model\n" " // event set individually instead of catching event for all the models.\n" " $event = \"eloquent.{$event}: \".static::class;\n\n" " $method = $halt ? 'until' : 'fire';\n\n" " return static::$dispatcher->$method($event, $this);\n" " }\n\n" " /**\n" " * Set the keys for a save update query.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Builder $query\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder\n" " */\n" " protected function setKeysForSaveQuery(Builder $query)\n" " {\n" " $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());\n\n" " return $query;\n" " }\n\n" " /**\n" " * Get the primary key value for a save query.\n" " *\n" " * @return mixed\n" " */\n" " protected function getKeyForSaveQuery()\n" " {\n" " if (isset($this->original[$this->getKeyName()])) {\n" " return $this->original[$this->getKeyName()];\n" " }\n\n" " return $this->getAttribute($this->getKeyName());\n" " }\n\n" " /**\n" " * Update the model's update timestamp.\n" " *\n" " * @return bool\n" " */\n" " public function touch()\n" " {\n" " if (! $this->timestamps) {\n" " return false;\n" " }\n\n" " $this->updateTimestamps();\n\n" " return $this->save();\n" " }\n\n" " /**\n" " * Update the creation and update timestamps.\n" " *\n" " * @return void\n" " */\n" " protected function updateTimestamps()\n" " {\n" " $time = $this->freshTimestamp();\n\n" " if (! $this->isDirty(static::UPDATED_AT)) {\n" " $this->setUpdatedAt($time);\n" " }\n\n" " if (! $this->exists && ! $this->isDirty(static::CREATED_AT)) {\n" " $this->setCreatedAt($time);\n" " }\n" " }\n\n" " /**\n" " * Set the value of the \"created at\" attribute.\n" " *\n" " * @param mixed $value\n" " * @return $this\n" " */\n" " public function setCreatedAt($value)\n" " {\n" " $this->{static::CREATED_AT} = $value;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Set the value of the \"updated at\" attribute.\n" " *\n" " * @param mixed $value\n" " * @return $this\n" " */\n" " public function setUpdatedAt($value)\n" " {\n" " $this->{static::UPDATED_AT} = $value;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the name of the \"created at\" column.\n" " *\n" " * @return string\n" " */\n" " public function getCreatedAtColumn()\n" " {\n" " return static::CREATED_AT;\n" " }\n\n" " /**\n" " * Get the name of the \"updated at\" column.\n" " *\n" " * @return string\n" " */\n" " public function getUpdatedAtColumn()\n" " {\n" " return static::UPDATED_AT;\n" " }\n\n" " /**\n" " * Get a fresh timestamp for the model.\n" " *\n" " * @return \\Carbon\\Carbon\n" " */\n" " public function freshTimestamp()\n" " {\n" " return new Carbon;\n" " }\n\n" " /**\n" " * Get a fresh timestamp for the model.\n" " *\n" " * @return string\n" " */\n" " public function freshTimestampString()\n" " {\n" " return $this->fromDateTime($this->freshTimestamp());\n" " }\n\n" " /**\n" " * Get a new query builder for the model's table.\n" " *\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder\n" " */\n" " public function newQuery()\n" " {\n" " $builder = $this->newQueryWithoutScopes();\n\n" " foreach ($this->getGlobalScopes() as $identifier => $scope) {\n" " $builder->withGlobalScope($identifier, $scope);\n" " }\n\n" " return $builder;\n" " }\n\n" " /**\n" " * Get a new query instance without a given scope.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Scope|string $scope\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder\n" " */\n" " public function newQueryWithoutScope($scope)\n" " {\n" " $builder = $this->newQuery();\n\n" " return $builder->withoutGlobalScope($scope);\n" " }\n\n" " /**\n" " * Get a new query builder that doesn't have any global scopes.\n" " *\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder|static\n" " */\n" " public function newQueryWithoutScopes()\n" " {\n" " $builder = $this->newEloquentBuilder(\n" " $this->newBaseQueryBuilder()\n" " );\n\n" " // Once we have the query builders, we will set the model instances so the\n" " // builder can easily access any information it may need from the model\n" " // while it is constructing and executing various queries against it.\n" " return $builder->setModel($this)->with($this->with);\n" " }\n\n" " /**\n" " * Create a new Eloquent query builder for the model.\n" " *\n" " * @param \\Illuminate\\Database\\Query\\Builder $query\n" " * @return \\Illuminate\\Database\\Eloquent\\Builder|static\n" " */\n" " public function newEloquentBuilder($query)\n" " {\n" " return new Builder($query);\n" " }\n\n" " /**\n" " * Get a new query builder instance for the connection.\n" " *\n" " * @return \\Illuminate\\Database\\Query\\Builder\n" " */\n" " protected function newBaseQueryBuilder()\n" " {\n" " $conn = $this->getConnection();\n\n" " $grammar = $conn->getQueryGrammar();\n\n" " return new QueryBuilder($conn, $grammar, $conn->getPostProcessor());\n" " }\n\n" " /**\n" " * Create a new Eloquent Collection instance.\n" " *\n" " * @param array $models\n" " * @return \\Illuminate\\Database\\Eloquent\\Collection\n" " */\n" " public function newCollection(array $models = [])\n" " {\n" " return new Collection($models);\n" " }\n\n" " /**\n" " * Create a new pivot model instance.\n" " *\n" " * @param \\Illuminate\\Database\\Eloquent\\Model $parent\n" " * @param array $attributes\n" " * @param string $table\n" " * @param bool $exists\n" " * @return \\Illuminate\\Database\\Eloquent\\Relations\\Pivot\n" " */\n" " public function newPivot(Model $parent, array $attributes, $table, $exists)\n" " {\n" " return new Pivot($parent, $attributes, $table, $exists);\n" " }\n\n" " /**\n" " * Get the table associated with the model.\n" " *\n" " * @return string\n" " */\n" " public function getTable()\n" " {\n" " if (isset($this->table)) {\n" " return $this->table;\n" " }\n\n" " return str_replace('\\\\', '', Str::snake(Str::plural(class_basename($this))));\n" " }\n\n" " /**\n" " * Set the table associated with the model.\n" " *\n" " * @param string $table\n" " * @return $this\n" " */\n" " public function setTable($table)\n" " {\n" " $this->table = $table;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the value of the model's primary key.\n" " *\n" " * @return mixed\n" " */\n" " public function getKey()\n" " {\n" " return $this->getAttribute($this->getKeyName());\n" " }\n\n" " /**\n" " * Get the queueable identity for the entity.\n" " *\n" " * @return mixed\n" " */\n" " public function getQueueableId()\n" " {\n" " return $this->getKey();\n" " }\n\n" " /**\n" " * Get the primary key for the model.\n" " *\n" " * @return string\n" " */\n" " public function getKeyName()\n" " {\n" " return $this->primaryKey;\n" " }\n\n" " /**\n" " * Set the primary key for the model.\n" " *\n" " * @param string $key\n" " * @return $this\n" " */\n" " public function setKeyName($key)\n" " {\n" " $this->primaryKey = $key;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the table qualified key name.\n" " *\n" " * @return string\n" " */\n" " public function getQualifiedKeyName()\n" " {\n" " return $this->getTable().'.'.$this->getKeyName();\n" " }\n\n" " /**\n" " * Get the value of the model's route key.\n" " *\n" " * @return mixed\n" " */\n" " public function getRouteKey()\n" " {\n" " return $this->getAttribute($this->getRouteKeyName());\n" " }\n\n" " /**\n" " * Get the route key for the model.\n" " *\n" " * @return string\n" " */\n" " public function getRouteKeyName()\n" " {\n" " return $this->getKeyName();\n" " }\n\n" " /**\n" " * Determine if the model uses timestamps.\n" " *\n" " * @return bool\n" " */\n" " public function usesTimestamps()\n" " {\n" " return $this->timestamps;\n" " }\n\n" " /**\n" " * Get the polymorphic relationship columns.\n" " *\n" " * @param string $name\n" " * @param string $type\n" " * @param string $id\n" " * @return array\n" " */\n" " protected function getMorphs($name, $type, $id)\n" " {\n" " $type = $type ?: $name.'_type';\n\n" " $id = $id ?: $name.'_id';\n\n" " return [$type, $id];\n" " }\n\n" " /**\n" " * Get the class name for polymorphic relations.\n" " *\n" " * @return string\n" " */\n" " public function getMorphClass()\n" " {\n" " $morphMap = Relation::morphMap();\n\n" " $class = static::class;\n\n" " if (! empty($morphMap) && in_array($class, $morphMap)) {\n" " return array_search($class, $morphMap, true);\n" " }\n\n" " return $this->morphClass ?: $class;\n" " }\n\n" " /**\n" " * Get the number of models to return per page.\n" " *\n" " * @return int\n" " */\n" " public function getPerPage()\n" " {\n" " return $this->perPage;\n" " }\n\n" " /**\n" " * Set the number of models to return per page.\n" " *\n" " * @param int $perPage\n" " * @return $this\n" " */\n" " public function setPerPage($perPage)\n" " {\n" " $this->perPage = $perPage;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the default foreign key name for the model.\n" " *\n" " * @return string\n" " */\n" " public function getForeignKey()\n" " {\n" " return Str::snake(class_basename($this)).'_id';\n" " }\n\n" " /**\n" " * Get the hidden attributes for the model.\n" " *\n" " * @return array\n" " */\n" " public function getHidden()\n" " {\n" " return $this->hidden;\n" " }\n\n" " /**\n" " * Set the hidden attributes for the model.\n" " *\n" " * @param array $hidden\n" " * @return $this\n" " */\n" " public function setHidden(array $hidden)\n" " {\n" " $this->hidden = $hidden;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Add hidden attributes for the model.\n" " *\n" " * @param array|string|null $attributes\n" " * @return void\n" " */\n" " public function addHidden($attributes = null)\n" " {\n" " $attributes = is_array($attributes) ? $attributes : func_get_args();\n\n" " $this->hidden = array_merge($this->hidden, $attributes);\n" " }\n\n" " /**\n" " * Make the given, typically hidden, attributes visible.\n" " *\n" " * @param array|string $attributes\n" " * @return $this\n" " */\n" " public function makeVisible($attributes)\n" " {\n" " $this->hidden = array_diff($this->hidden, (array) $attributes);\n\n" " if (! empty($this->visible)) {\n" " $this->addVisible($attributes);\n" " }\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the visible attributes for the model.\n" " *\n" " * @return array\n" " */\n" " public function getVisible()\n" " {\n" " return $this->visible;\n" " }\n\n" " /**\n" " * Set the visible attributes for the model.\n" " *\n" " * @param array $visible\n" " * @return $this\n" " */\n" " public function setVisible(array $visible)\n" " {\n" " $this->visible = $visible;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Add visible attributes for the model.\n" " *\n" " * @param array|string|null $attributes\n" " * @return void\n" " */\n" " public function addVisible($attributes = null)\n" " {\n" " $attributes = is_array($attributes) ? $attributes : func_get_args();\n\n" " $this->visible = array_merge($this->visible, $attributes);\n" " }\n\n" " /**\n" " * Set the accessors to append to model arrays.\n" " *\n" " * @param array $appends\n" " * @return $this\n" " */\n" " public function setAppends(array $appends)\n" " {\n" " $this->appends = $appends;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the fillable attributes for the model.\n" " *\n" " * @return array\n" " */\n" " public function getFillable()\n" " {\n" " return $this->fillable;\n" " }\n\n" " /**\n" " * Set the fillable attributes for the model.\n" " *\n" " * @param array $fillable\n" " * @return $this\n" " */\n" " public function fillable(array $fillable)\n" " {\n" " $this->fillable = $fillable;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the guarded attributes for the model.\n" " *\n" " * @return array\n" " */\n" " public function getGuarded()\n" " {\n" " return $this->guarded;\n" " }\n\n" " /**\n" " * Set the guarded attributes for the model.\n" " *\n" " * @param array $guarded\n" " * @return $this\n" " */\n" " public function guard(array $guarded)\n" " {\n" " $this->guarded = $guarded;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Disable all mass assignable restrictions.\n" " *\n" " * @param bool $state\n" " * @return void\n" " */\n" " public static function unguard($state = true)\n" " {\n" " static::$unguarded = $state;\n" " }\n\n" " /**\n" " * Enable the mass assignment restrictions.\n" " *\n" " * @return void\n" " */\n" " public static function reguard()\n" " {\n" " static::$unguarded = false;\n" " }\n\n" " /**\n" " * Determine if current state is \"unguarded\".\n" " *\n" " * @return bool\n" " */\n" " public static function isUnguarded()\n" " {\n" " return static::$unguarded;\n" " }\n\n" " /**\n" " * Run the given callable while being unguarded.\n" " *\n" " * @param callable $callback\n" " * @return mixed\n" " */\n" " public static function unguarded(callable $callback)\n" " {\n" " if (static::$unguarded) {\n" " return $callback();\n" " }\n\n" " static::unguard();\n\n" " try {\n" " return $callback();\n" " } finally {\n" " static::reguard();\n" " }\n" " }\n\n" " /**\n" " * Determine if the given attribute may be mass assigned.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " public function isFillable($key)\n" " {\n" " if (static::$unguarded) {\n" " return true;\n" " }\n\n" " // If the key is in the \"fillable\" array, we can of course assume that it's\n" " // a fillable attribute. Otherwise, we will check the guarded array when\n" " // we need to determine if the attribute is black-listed on the model.\n" " if (in_array($key, $this->getFillable())) {\n" " return true;\n" " }\n\n" " if ($this->isGuarded($key)) {\n" " return false;\n" " }\n\n" " return empty($this->getFillable()) && ! Str::startsWith($key, '_');\n" " }\n\n" " /**\n" " * Determine if the given key is guarded.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " public function isGuarded($key)\n" " {\n" " return in_array($key, $this->getGuarded()) || $this->getGuarded() == ['*'];\n" " }\n\n" " /**\n" " * Determine if the model is totally guarded.\n" " *\n" " * @return bool\n" " */\n" " public function totallyGuarded()\n" " {\n" " return count($this->getFillable()) == 0 && $this->getGuarded() == ['*'];\n" " }\n\n" " /**\n" " * Remove the table name from a given key.\n" " *\n" " * @param string $key\n" " * @return string\n" " */\n" " protected function removeTableFromKey($key)\n" " {\n" " if (! Str::contains($key, '.')) {\n" " return $key;\n" " }\n\n" " return last(explode('.', $key));\n" " }\n\n" " /**\n" " * Get the relationships that are touched on save.\n" " *\n" " * @return array\n" " */\n" " public function getTouchedRelations()\n" " {\n" " return $this->touches;\n" " }\n\n" " /**\n" " * Set the relationships that are touched on save.\n" " *\n" " * @param array $touches\n" " * @return $this\n" " */\n" " public function setTouchedRelations(array $touches)\n" " {\n" " $this->touches = $touches;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the value indicating whether the IDs are incrementing.\n" " *\n" " * @return bool\n" " */\n" " public function getIncrementing()\n" " {\n" " return $this->incrementing;\n" " }\n\n" " /**\n" " * Set whether IDs are incrementing.\n" " *\n" " * @param bool $value\n" " * @return $this\n" " */\n" " public function setIncrementing($value)\n" " {\n" " $this->incrementing = $value;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Convert the model instance to JSON.\n" " *\n" " * @param int $options\n" " * @return string\n" " */\n" " public function toJson($options = 0)\n" " {\n" " return json_encode($this->jsonSerialize(), $options);\n" " }\n\n" " /**\n" " * Convert the object into something JSON serializable.\n" " *\n" " * @return array\n" " */\n" " public function jsonSerialize()\n" " {\n" " return $this->toArray();\n" " }\n\n" " /**\n" " * Convert the model instance to an array.\n" " *\n" " * @return array\n" " */\n" " public function toArray()\n" " {\n" " $attributes = $this->attributesToArray();\n\n" " return array_merge($attributes, $this->relationsToArray());\n" " }\n\n" " /**\n" " * Convert the model's attributes to an array.\n" " *\n" " * @return array\n" " */\n" " public function attributesToArray()\n" " {\n" " $attributes = $this->getArrayableAttributes();\n\n" " // If an attribute is a date, we will cast it to a string after converting it\n" " // to a DateTime / Carbon instance. This is so we will get some consistent\n" " // formatting while accessing attributes vs. arraying / JSONing a model.\n" " foreach ($this->getDates() as $key) {\n" " if (! isset($attributes[$key])) {\n" " continue;\n" " }\n\n" " $attributes[$key] = $this->serializeDate(\n" " $this->asDateTime($attributes[$key])\n" " );\n" " }\n\n" " $mutatedAttributes = $this->getMutatedAttributes();\n\n" " // We want to spin through all the mutated attributes for this model and call\n" " // the mutator for the attribute. We cache off every mutated attributes so\n" " // we don't have to constantly check on attributes that actually change.\n" " foreach ($mutatedAttributes as $key) {\n" " if (! array_key_exists($key, $attributes)) {\n" " continue;\n" " }\n\n" " $attributes[$key] = $this->mutateAttributeForArray(\n" " $key, $attributes[$key]\n" " );\n" " }\n\n" " // Next we will handle any casts that have been setup for this model and cast\n" " // the values to their appropriate type. If the attribute has a mutator we\n" " // will not perform the cast on those attributes to avoid any confusion.\n" " foreach ($this->getCasts() as $key => $value) {\n" " if (! array_key_exists($key, $attributes) ||\n" " in_array($key, $mutatedAttributes)) {\n" " continue;\n" " }\n\n" " $attributes[$key] = $this->castAttribute(\n" " $key, $attributes[$key]\n" " );\n\n" " if ($attributes[$key] && ($value === 'date' || $value === 'datetime')) {\n" " $attributes[$key] = $this->serializeDate($attributes[$key]);\n" " }\n" " }\n\n" " // Here we will grab all of the appended, calculated attributes to this model\n" " // as these attributes are not really in the attributes array, but are run\n" " // when we need to array or JSON the model for convenience to the coder.\n" " foreach ($this->getArrayableAppends() as $key) {\n" " $attributes[$key] = $this->mutateAttributeForArray($key, null);\n" " }\n\n" " return $attributes;\n" " }\n\n" " /**\n" " * Get an attribute array of all arrayable attributes.\n" " *\n" " * @return array\n" " */\n" " protected function getArrayableAttributes()\n" " {\n" " return $this->getArrayableItems($this->attributes);\n" " }\n\n" " /**\n" " * Get all of the appendable values that are arrayable.\n" " *\n" " * @return array\n" " */\n" " protected function getArrayableAppends()\n" " {\n" " if (! count($this->appends)) {\n" " return [];\n" " }\n\n" " return $this->getArrayableItems(\n" " array_combine($this->appends, $this->appends)\n" " );\n" " }\n\n" " /**\n" " * Get the model's relationships in array form.\n" " *\n" " * @return array\n" " */\n" " public function relationsToArray()\n" " {\n" " $attributes = [];\n\n" " foreach ($this->getArrayableRelations() as $key => $value) {\n" " // If the values implements the Arrayable interface we can just call this\n" " // toArray method on the instances which will convert both models and\n" " // collections to their proper array form and we'll set the values.\n" " if ($value instanceof Arrayable) {\n" " $relation = $value->toArray();\n" " }\n\n" " // If the value is null, we'll still go ahead and set it in this list of\n" " // attributes since null is used to represent empty relationships if\n" " // if it a has one or belongs to type relationships on the models.\n" " elseif (is_null($value)) {\n" " $relation = $value;\n" " }\n\n" " // If the relationships snake-casing is enabled, we will snake case this\n" " // key so that the relation attribute is snake cased in this returned\n" " // array to the developers, making this consistent with attributes.\n" " if (static::$snakeAttributes) {\n" " $key = Str::snake($key);\n" " }\n\n" " // If the relation value has been set, we will set it on this attributes\n" " // list for returning. If it was not arrayable or null, we'll not set\n" " // the value on the array because it is some type of invalid value.\n" " if (isset($relation) || is_null($value)) {\n" " $attributes[$key] = $relation;\n" " }\n\n" " unset($relation);\n" " }\n\n" " return $attributes;\n" " }\n\n" " /**\n" " * Get an attribute array of all arrayable relations.\n" " *\n" " * @return array\n" " */\n" " protected function getArrayableRelations()\n" " {\n" " return $this->getArrayableItems($this->relations);\n" " }\n\n" " /**\n" " * Get an attribute array of all arrayable values.\n" " *\n" " * @param array $values\n" " * @return array\n" " */\n" " protected function getArrayableItems(array $values)\n" " {\n" " if (count($this->getVisible()) > 0) {\n" " return array_intersect_key($values, array_flip($this->getVisible()));\n" " }\n\n" " return array_diff_key($values, array_flip($this->getHidden()));\n" " }\n\n" " /**\n" " * Get an attribute from the model.\n" " *\n" " * @param string $key\n" " * @return mixed\n" " */\n" " public function getAttribute($key)\n" " {\n" " if (array_key_exists($key, $this->attributes) || $this->hasGetMutator($key)) {\n" " return $this->getAttributeValue($key);\n" " }\n\n" " return $this->getRelationValue($key);\n" " }\n\n" " /**\n" " * Get a plain attribute (not a relationship).\n" " *\n" " * @param string $key\n" " * @return mixed\n" " */\n" " public function getAttributeValue($key)\n" " {\n" " $value = $this->getAttributeFromArray($key);\n\n" " // If the attribute has a get mutator, we will call that then return what\n" " // it returns as the value, which is useful for transforming values on\n" " // retrieval from the model to a form that is more useful for usage.\n" " if ($this->hasGetMutator($key)) {\n" " return $this->mutateAttribute($key, $value);\n" " }\n\n" " // If the attribute exists within the cast array, we will convert it to\n" " // an appropriate native PHP type dependant upon the associated value\n" " // given with the key in the pair. Dayle made this comment line up.\n" " if ($this->hasCast($key)) {\n" " return $this->castAttribute($key, $value);\n" " }\n\n" " // If the attribute is listed as a date, we will convert it to a DateTime\n" " // instance on retrieval, which makes it quite convenient to work with\n" " // date fields without having to create a mutator for each property.\n" " if (in_array($key, $this->getDates()) && ! is_null($value)) {\n" " return $this->asDateTime($value);\n" " }\n\n" " return $value;\n" " }\n\n" " /**\n" " * Get a relationship.\n" " *\n" " * @param string $key\n" " * @return mixed\n" " */\n" " public function getRelationValue($key)\n" " {\n" " // If the key already exists in the relationships array, it just means the\n" " // relationship has already been loaded, so we'll just return it out of\n" " // here because there is no need to query within the relations twice.\n" " if ($this->relationLoaded($key)) {\n" " return $this->relations[$key];\n" " }\n\n" " // If the \"attribute\" exists as a method on the model, we will just assume\n" " // it is a relationship and will load and return results from the query\n" " // and hydrate the relationship's value on the \"relationships\" array.\n" " if (method_exists($this, $key)) {\n" " return $this->getRelationshipFromMethod($key);\n" " }\n" " }\n\n" " /**\n" " * Get an attribute from the $attributes array.\n" " *\n" " * @param string $key\n" " * @return mixed\n" " */\n" " protected function getAttributeFromArray($key)\n" " {\n" " if (array_key_exists($key, $this->attributes)) {\n" " return $this->attributes[$key];\n" " }\n" " }\n\n" " /**\n" " * Get a relationship value from a method.\n" " *\n" " * @param string $method\n" " * @return mixed\n" " *\n" " * @throws \\LogicException\n" " */\n" " protected function getRelationshipFromMethod($method)\n" " {\n" " $relations = $this->$method();\n\n" " if (! $relations instanceof Relation) {\n" " throw new LogicException('Relationship method must return an object of type '\n" " .'Illuminate\\Database\\Eloquent\\Relations\\Relation');\n" " }\n\n" " $this->setRelation($method, $results = $relations->getResults());\n\n" " return $results;\n" " }\n\n" " /**\n" " * Determine if a get mutator exists for an attribute.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " public function hasGetMutator($key)\n" " {\n" " return method_exists($this, 'get'.Str::studly($key).'Attribute');\n" " }\n\n" " /**\n" " * Get the value of an attribute using its mutator.\n" " *\n" " * @param string $key\n" " * @param mixed $value\n" " * @return mixed\n" " */\n" " protected function mutateAttribute($key, $value)\n" " {\n" " return $this->{'get'.Str::studly($key).'Attribute'}($value);\n" " }\n\n" " /**\n" " * Get the value of an attribute using its mutator for array conversion.\n" " *\n" " * @param string $key\n" " * @param mixed $value\n" " * @return mixed\n" " */\n" " protected function mutateAttributeForArray($key, $value)\n" " {\n" " $value = $this->mutateAttribute($key, $value);\n\n" " return $value instanceof Arrayable ? $value->toArray() : $value;\n" " }\n\n" " /**\n" " * Determine whether an attribute should be cast to a native type.\n" " *\n" " * @param string $key\n" " * @param array|string|null $types\n" " * @return bool\n" " */\n" " public function hasCast($key, $types = null)\n" " {\n" " if (array_key_exists($key, $this->getCasts())) {\n" " return $types ? in_array($this->getCastType($key), (array) $types, true) : true;\n" " }\n\n" " return false;\n" " }\n\n" " /**\n" " * Get the casts array.\n" " *\n" " * @return array\n" " */\n" " public function getCasts()\n" " {\n" " if ($this->getIncrementing()) {\n" " return array_merge([\n" " $this->getKeyName() => $this->keyType,\n" " ], $this->casts);\n" " }\n\n" " return $this->casts;\n" " }\n\n" " /**\n" " * Determine whether a value is Date / DateTime castable for inbound manipulation.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " protected function isDateCastable($key)\n" " {\n" " return $this->hasCast($key, ['date', 'datetime']);\n" " }\n\n" " /**\n" " * Determine whether a value is JSON castable for inbound manipulation.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " protected function isJsonCastable($key)\n" " {\n" " return $this->hasCast($key, ['array', 'json', 'object', 'collection']);\n" " }\n\n" " /**\n" " * Get the type of cast for a model attribute.\n" " *\n" " * @param string $key\n" " * @return string\n" " */\n" " protected function getCastType($key)\n" " {\n" " return trim(strtolower($this->getCasts()[$key]));\n" " }\n\n" " /**\n" " * Cast an attribute to a native PHP type.\n" " *\n" " * @param string $key\n" " * @param mixed $value\n" " * @return mixed\n" " */\n" " protected function castAttribute($key, $value)\n" " {\n" " if (is_null($value)) {\n" " return $value;\n" " }\n\n" " switch ($this->getCastType($key)) {\n" " case 'int':\n" " case 'integer':\n" " return (int) $value;\n" " case 'real':\n" " case 'float':\n" " case 'double':\n" " return (float) $value;\n" " case 'string':\n" " return (string) $value;\n" " case 'bool':\n" " case 'boolean':\n" " return (bool) $value;\n" " case 'object':\n" " return $this->fromJson($value, true);\n" " case 'array':\n" " case 'json':\n" " return $this->fromJson($value);\n" " case 'collection':\n" " return new BaseCollection($this->fromJson($value));\n" " case 'date':\n" " case 'datetime':\n" " return $this->asDateTime($value);\n" " case 'timestamp':\n" " return $this->asTimeStamp($value);\n" " default:\n" " return $value;\n" " }\n" " }\n\n" " /**\n" " * Set a given attribute on the model.\n" " *\n" " * @param string $key\n" " * @param mixed $value\n" " * @return $this\n" " */\n" " public function setAttribute($key, $value)\n" " {\n" " // First we will check for the presence of a mutator for the set operation\n" " // which simply lets the developers tweak the attribute as it is set on\n" " // the model, such as \"json_encoding\" an listing of data for storage.\n" " if ($this->hasSetMutator($key)) {\n" " $method = 'set'.Str::studly($key).'Attribute';\n\n" " return $this->{$method}($value);\n" " }\n\n" " // If an attribute is listed as a \"date\", we'll convert it from a DateTime\n" " // instance into a form proper for storage on the database tables using\n" " // the connection grammar's date format. We will auto set the values.\n" " elseif ($value && (in_array($key, $this->getDates()) || $this->isDateCastable($key))) {\n" " $value = $this->fromDateTime($value);\n" " }\n\n" " if ($this->isJsonCastable($key) && ! is_null($value)) {\n" " $value = $this->asJson($value);\n" " }\n\n" " $this->attributes[$key] = $value;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Determine if a set mutator exists for an attribute.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " public function hasSetMutator($key)\n" " {\n" " return method_exists($this, 'set'.Str::studly($key).'Attribute');\n" " }\n\n" " /**\n" " * Get the attributes that should be converted to dates.\n" " *\n" " * @return array\n" " */\n" " public function getDates()\n" " {\n" " $defaults = [static::CREATED_AT, static::UPDATED_AT];\n\n" " return $this->timestamps ? array_merge($this->dates, $defaults) : $this->dates;\n" " }\n\n" " /**\n" " * Convert a DateTime to a storable string.\n" " *\n" " * @param \\DateTime|int $value\n" " * @return string\n" " */\n" " public function fromDateTime($value)\n" " {\n" " $format = $this->getDateFormat();\n\n" " $value = $this->asDateTime($value);\n\n" " return $value->format($format);\n" " }\n\n" " /**\n" " * Return a timestamp as DateTime object.\n" " *\n" " * @param mixed $value\n" " * @return \\Carbon\\Carbon\n" " */\n" " protected function asDateTime($value)\n" " {\n" " // If this value is already a Carbon instance, we shall just return it as is.\n" " // This prevents us having to re-instantiate a Carbon instance when we know\n" " // it already is one, which wouldn't be fulfilled by the DateTime check.\n" " if ($value instanceof Carbon) {\n" " return $value;\n" " }\n\n" " // If the value is already a DateTime instance, we will just skip the rest of\n" " // these checks since they will be a waste of time, and hinder performance\n" " // when checking the field. We will just return the DateTime right away.\n" " if ($value instanceof DateTimeInterface) {\n" " return new Carbon(\n" " $value->format('Y-m-d H:i:s.u'), $value->getTimeZone()\n" " );\n" " }\n\n" " // If this value is an integer, we will assume it is a UNIX timestamp's value\n" " // and format a Carbon object from this timestamp. This allows flexibility\n" " // when defining your date fields as they might be UNIX timestamps here.\n" " if (is_numeric($value)) {\n" " return Carbon::createFromTimestamp($value);\n" " }\n\n" " // If the value is in simply year, month, day format, we will instantiate the\n" " // Carbon instances from that format. Again, this provides for simple date\n" " // fields on the database, while still supporting Carbonized conversion.\n" " if (preg_match('/^(\\d{4})-(\\d{1,2})-(\\d{1,2})$/', $value)) {\n" " return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();\n" " }\n\n" " // Finally, we will just assume this date is in the format used by default on\n" " // the database connection and use that format to create the Carbon object\n" " // that is returned back out to the developers after we convert it here.\n" " return Carbon::createFromFormat($this->getDateFormat(), $value);\n" " }\n\n" " /**\n" " * Return a timestamp as unix timestamp.\n" " *\n" " * @param mixed $value\n" " * @return int\n" " */\n" " protected function asTimeStamp($value)\n" " {\n" " return $this->asDateTime($value)->getTimestamp();\n" " }\n\n" " /**\n" " * Prepare a date for array / JSON serialization.\n" " *\n" " * @param \\DateTimeInterface $date\n" " * @return string\n" " */\n" " protected function serializeDate(DateTimeInterface $date)\n" " {\n" " return $date->format($this->getDateFormat());\n" " }\n\n" " /**\n" " * Get the format for database stored dates.\n" " *\n" " * @return string\n" " */\n" " protected function getDateFormat()\n" " {\n" " return $this->dateFormat ?: $this->getConnection()->getQueryGrammar()->getDateFormat();\n" " }\n\n" " /**\n" " * Set the date format used by the model.\n" " *\n" " * @param string $format\n" " * @return $this\n" " */\n" " public function setDateFormat($format)\n" " {\n" " $this->dateFormat = $format;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Encode the given value as JSON.\n" " *\n" " * @param mixed $value\n" " * @return string\n" " */\n" " protected function asJson($value)\n" " {\n" " return json_encode($value);\n" " }\n\n" " /**\n" " * Decode the given JSON back into an array or object.\n" " *\n" " * @param string $value\n" " * @param bool $asObject\n" " * @return mixed\n" " */\n" " public function fromJson($value, $asObject = false)\n" " {\n" " return json_decode($value, ! $asObject);\n" " }\n\n" " /**\n" " * Clone the model into a new, non-existing instance.\n" " *\n" " * @param array|null $except\n" " * @return \\Illuminate\\Database\\Eloquent\\Model\n" " */\n" " public function replicate(array $except = null)\n" " {\n" " $defaults = [\n" " $this->getKeyName(),\n" " $this->getCreatedAtColumn(),\n" " $this->getUpdatedAtColumn(),\n" " ];\n\n" " $except = $except ? array_unique(array_merge($except, $defaults)) : $defaults;\n\n" " $attributes = Arr::except($this->attributes, $except);\n\n" " $instance = new static;\n\n" " $instance->setRawAttributes($attributes);\n\n" " return $instance->setRelations($this->relations);\n" " }\n\n" " /**\n" " * Get all of the current attributes on the model.\n" " *\n" " * @return array\n" " */\n" " public function getAttributes()\n" " {\n" " return $this->attributes;\n" " }\n\n" " /**\n" " * Set the array of model attributes. No checking is done.\n" " *\n" " * @param array $attributes\n" " * @param bool $sync\n" " * @return $this\n" " */\n" " public function setRawAttributes(array $attributes, $sync = false)\n" " {\n" " $this->attributes = $attributes;\n\n" " if ($sync) {\n" " $this->syncOriginal();\n" " }\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the model's original attribute values.\n" " *\n" " * @param string|null $key\n" " * @param mixed $default\n" " * @return mixed|array\n" " */\n" " public function getOriginal($key = null, $default = null)\n" " {\n" " return Arr::get($this->original, $key, $default);\n" " }\n\n" " /**\n" " * Sync the original attributes with the current.\n" " *\n" " * @return $this\n" " */\n" " public function syncOriginal()\n" " {\n" " $this->original = $this->attributes;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Sync a single original attribute with its current value.\n" " *\n" " * @param string $attribute\n" " * @return $this\n" " */\n" " public function syncOriginalAttribute($attribute)\n" " {\n" " $this->original[$attribute] = $this->attributes[$attribute];\n\n" " return $this;\n" " }\n\n" " /**\n" " * Determine if the model or given attribute(s) have been modified.\n" " *\n" " * @param array|string|null $attributes\n" " * @return bool\n" " */\n" " public function isDirty($attributes = null)\n" " {\n" " $dirty = $this->getDirty();\n\n" " if (is_null($attributes)) {\n" " return count($dirty) > 0;\n" " }\n\n" " if (! is_array($attributes)) {\n" " $attributes = func_get_args();\n" " }\n\n" " foreach ($attributes as $attribute) {\n" " if (array_key_exists($attribute, $dirty)) {\n" " return true;\n" " }\n" " }\n\n" " return false;\n" " }\n\n" " /**\n" " * Get the attributes that have been changed since last sync.\n" " *\n" " * @return array\n" " */\n" " public function getDirty()\n" " {\n" " $dirty = [];\n\n" " foreach ($this->attributes as $key => $value) {\n" " if (! array_key_exists($key, $this->original)) {\n" " $dirty[$key] = $value;\n" " } elseif ($value !== $this->original[$key] &&\n" " ! $this->originalIsNumericallyEquivalent($key)) {\n" " $dirty[$key] = $value;\n" " }\n" " }\n\n" " return $dirty;\n" " }\n\n" " /**\n" " * Determine if the new and old values for a given key are numerically equivalent.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " protected function originalIsNumericallyEquivalent($key)\n" " {\n" " $current = $this->attributes[$key];\n\n" " $original = $this->original[$key];\n\n" " return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0;\n" " }\n\n" " /**\n" " * Get all the loaded relations for the instance.\n" " *\n" " * @return array\n" " */\n" " public function getRelations()\n" " {\n" " return $this->relations;\n" " }\n\n" " /**\n" " * Get a specified relationship.\n" " *\n" " * @param string $relation\n" " * @return mixed\n" " */\n" " public function getRelation($relation)\n" " {\n" " return $this->relations[$relation];\n" " }\n\n" " /**\n" " * Determine if the given relation is loaded.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " public function relationLoaded($key)\n" " {\n" " return array_key_exists($key, $this->relations);\n" " }\n\n" " /**\n" " * Set the specific relationship in the model.\n" " *\n" " * @param string $relation\n" " * @param mixed $value\n" " * @return $this\n" " */\n" " public function setRelation($relation, $value)\n" " {\n" " $this->relations[$relation] = $value;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Set the entire relations array on the model.\n" " *\n" " * @param array $relations\n" " * @return $this\n" " */\n" " public function setRelations(array $relations)\n" " {\n" " $this->relations = $relations;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Get the database connection for the model.\n" " *\n" " * @return \\Illuminate\\Database\\Connection\n" " */\n" " public function getConnection()\n" " {\n" " return static::resolveConnection($this->getConnectionName());\n" " }\n\n" " /**\n" " * Get the current connection name for the model.\n" " *\n" " * @return string\n" " */\n" " public function getConnectionName()\n" " {\n" " return $this->connection;\n" " }\n\n" " /**\n" " * Set the connection associated with the model.\n" " *\n" " * @param string $name\n" " * @return $this\n" " */\n" " public function setConnection($name)\n" " {\n" " $this->connection = $name;\n\n" " return $this;\n" " }\n\n" " /**\n" " * Resolve a connection instance.\n" " *\n" " * @param string|null $connection\n" " * @return \\Illuminate\\Database\\Connection\n" " */\n" " public static function resolveConnection($connection = null)\n" " {\n" " return static::$resolver->connection($connection);\n" " }\n\n" " /**\n" " * Get the connection resolver instance.\n" " *\n" " * @return \\Illuminate\\Database\\ConnectionResolverInterface\n" " */\n" " public static function getConnectionResolver()\n" " {\n" " return static::$resolver;\n" " }\n\n" " /**\n" " * Set the connection resolver instance.\n" " *\n" " * @param \\Illuminate\\Database\\ConnectionResolverInterface $resolver\n" " * @return void\n" " */\n" " public static function setConnectionResolver(Resolver $resolver)\n" " {\n" " static::$resolver = $resolver;\n" " }\n\n" " /**\n" " * Unset the connection resolver for models.\n" " *\n" " * @return void\n" " */\n" " public static function unsetConnectionResolver()\n" " {\n" " static::$resolver = null;\n" " }\n\n" " /**\n" " * Get the event dispatcher instance.\n" " *\n" " * @return \\Illuminate\\Contracts\\Events\\Dispatcher\n" " */\n" " public static function getEventDispatcher()\n" " {\n" " return static::$dispatcher;\n" " }\n\n" " /**\n" " * Set the event dispatcher instance.\n" " *\n" " * @param \\Illuminate\\Contracts\\Events\\Dispatcher $dispatcher\n" " * @return void\n" " */\n" " public static function setEventDispatcher(Dispatcher $dispatcher)\n" " {\n" " static::$dispatcher = $dispatcher;\n" " }\n\n" " /**\n" " * Unset the event dispatcher for models.\n" " *\n" " * @return void\n" " */\n" " public static function unsetEventDispatcher()\n" " {\n" " static::$dispatcher = null;\n" " }\n\n" " /**\n" " * Get the mutated attributes for a given instance.\n" " *\n" " * @return array\n" " */\n" " public function getMutatedAttributes()\n" " {\n" " $class = static::class;\n\n" " if (! isset(static::$mutatorCache[$class])) {\n" " static::cacheMutatedAttributes($class);\n" " }\n\n" " return static::$mutatorCache[$class];\n" " }\n\n" " /**\n" " * Extract and cache all the mutated attributes of a class.\n" " *\n" " * @param string $class\n" " * @return void\n" " */\n" " public static function cacheMutatedAttributes($class)\n" " {\n" " $mutatedAttributes = [];\n\n" " // Here we will extract all of the mutated attributes so that we can quickly\n" " // spin through them after we export models to their array form, which we\n" " // need to be fast. This'll let us know the attributes that can mutate.\n" " if (preg_match_all('/(?<=^|;)get([^;]+?)Attribute(;|$)/', implode(';', get_class_methods($class)), $matches)) {\n" " foreach ($matches[1] as $match) {\n" " if (static::$snakeAttributes) {\n" " $match = Str::snake($match);\n" " }\n\n" " $mutatedAttributes[] = lcfirst($match);\n" " }\n" " }\n\n" " static::$mutatorCache[$class] = $mutatedAttributes;\n" " }\n\n" " /**\n" " * Dynamically retrieve attributes on the model.\n" " *\n" " * @param string $key\n" " * @return mixed\n" " */\n" " public function __get($key)\n" " {\n" " return $this->getAttribute($key);\n" " }\n\n" " /**\n" " * Dynamically set attributes on the model.\n" " *\n" " * @param string $key\n" " * @param mixed $value\n" " * @return void\n" " */\n" " public function __set($key, $value)\n" " {\n" " $this->setAttribute($key, $value);\n" " }\n\n" " /**\n" " * Determine if the given attribute exists.\n" " *\n" " * @param mixed $offset\n" " * @return bool\n" " */\n" " public function offsetExists($offset)\n" " {\n" " return isset($this->$offset);\n" " }\n\n" " /**\n" " * Get the value for a given offset.\n" " *\n" " * @param mixed $offset\n" " * @return mixed\n" " */\n" " public function offsetGet($offset)\n" " {\n" " return $this->$offset;\n" " }\n\n" " /**\n" " * Set the value for a given offset.\n" " *\n" " * @param mixed $offset\n" " * @param mixed $value\n" " * @return void\n" " */\n" " public function offsetSet($offset, $value)\n" " {\n" " $this->$offset = $value;\n" " }\n\n" " /**\n" " * Unset the value for a given offset.\n" " *\n" " * @param mixed $offset\n" " * @return void\n" " */\n" " public function offsetUnset($offset)\n" " {\n" " unset($this->$offset);\n" " }\n\n" " /**\n" " * Determine if an attribute or relation exists on the model.\n" " *\n" " * @param string $key\n" " * @return bool\n" " */\n" " public function __isset($key)\n" " {\n" " return ! is_null($this->getAttribute($key));\n" " }\n\n" " /**\n" " * Unset an attribute on the model.\n" " *\n" " * @param string $key\n" " * @return void\n" " */\n" " public function __unset($key)\n" " {\n" " unset($this->attributes[$key], $this->relations[$key]);\n" " }\n\n" " /**\n" " * Handle dynamic method calls into the model.\n" " *\n" " * @param string $method\n" " * @param array $parameters\n" " * @return mixed\n" " */\n" " public function __call($method, $parameters)\n" " {\n" " if (in_array($method, ['increment', 'decrement'])) {\n" " return call_user_func_array([$this, $method], $parameters);\n" " }\n\n" " $query = $this->newQuery();\n\n" " return call_user_func_array([$query, $method], $parameters);\n" " }\n\n" " /**\n" " * Handle dynamic static method calls into the method.\n" " *\n" " * @param string $method\n" " * @param array $parameters\n" " * @return mixed\n" " */\n" " public static function __callStatic($method, $parameters)\n" " {\n" " $instance = new static;\n\n" " return call_user_func_array([$instance, $method], $parameters);\n" " }\n\n" " /**\n" " * Convert the model to its string representation.\n" " *\n" " * @return string\n" " */\n" " public function __toString()\n" " {\n" " return $this->toJson();\n" " }\n\n" " /**\n" " * When a model is being unserialized, check if it needs to be booted.\n" " *\n" " * @return void\n" " */\n" " public function __wakeup()\n" " {\n" " $this->bootIfNotBooted();\n" " }\n" "}") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html