Skip to content

Commit

Permalink
prevent considering an array attribute as callable while building mo…
Browse files Browse the repository at this point in the history
…del factories (#23372)
  • Loading branch information
themsaid authored and taylorotwell committed Mar 2, 2018
1 parent 83dd8cd commit 90f785a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected function stateAttributes($state, array $attributes)
protected function expandAttributes(array $attributes)
{
foreach ($attributes as &$attribute) {
if (is_callable($attribute) && ! is_string($attribute)) {
if (is_callable($attribute) && ! is_string($attribute) && ! is_array($attribute)) {
$attribute = $attribute($attributes);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Integration/Database/EloquentFactoryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function getEnvironmentSetUp($app)
return [
'name' => $faker->name,
'status' => 'active',
'tags' => ['Storage', 'Data'],
'user_id' => function () {
return factory(FactoryBuildableUser::class)->create()->id;
},
Expand Down Expand Up @@ -80,6 +81,7 @@ public function setUp()
Schema::create('servers', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('tags');
$table->integer('user_id');
$table->string('status');
});
Expand Down Expand Up @@ -134,6 +136,7 @@ public function creating_models_with_callable_states()
$callableServer = factory(FactoryBuildableServer::class)->states('callable')->create();

$this->assertEquals('active', $server->status);
$this->assertEquals(['Storage', 'Data'], $server->tags);
$this->assertEquals('callable', $callableServer->status);
}

Expand Down Expand Up @@ -208,6 +211,7 @@ class FactoryBuildableServer extends Model
public $table = 'servers';
public $timestamps = false;
protected $guarded = ['id'];
public $casts = ['tags' => 'array'];

public function user()
{
Expand Down

0 comments on commit 90f785a

Please sign in to comment.