Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[13.x] Do not allow new model instances to be created during boot #55685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion 18 src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayAccess;
use Closure;
use Exception;
use Illuminate\Contracts\Broadcasting\HasBroadcastChannel;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Contracts\Queue\QueueableEntity;
Expand Down Expand Up @@ -143,6 +144,13 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
*/
protected static $dispatcher;

/**
* The models that are currently being booted.
*
* @var array
*/
protected static $booting = [];

/**
* The array of booted models.
*
Expand Down Expand Up @@ -286,12 +294,20 @@ public function __construct(array $attributes = [])
protected function bootIfNotBooted()
{
if (! isset(static::$booted[static::class])) {
static::$booted[static::class] = true;
if (isset(static::$booting[static::class])) {
throw new LogicException('The ['.__METHOD__.'] method may not be called on model ['.static::class.'] while it is being booted.');
}

static::$booting[static::class] = true;

$this->fireModelEvent('booting', false);

static::booting();
static::boot();

static::$booted[static::class] = true;
unset(static::$booting[static::class]);

static::booted();

static::$bootedCallbacks[static::class] ??= [];
Expand Down
15 changes: 15 additions & 0 deletions 15 tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3330,6 +3330,21 @@ public function testUseFactoryAttribute()
$this->assertEquals(EloquentModelWithUseFactoryAttribute::class, $factory->modelName());
$this->assertEquals('test name', $instance->name); // Small smoke test to ensure the factory is working
}

public function testNestedModelBootingIsDisallowed()
{
$this->expectExceptionMessageMatches('/The \[(.+)] method may not be called on model \[(.+)\] while it is being booted\./');

$model = new class extends Model
{
protected static function boot()
{
parent::boot();

$tableName = (new static())->getTable();
}
};
}
}

class EloquentTestObserverStub
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.