Skip to content
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

[8.x] Callable attributes are called with expanded definition #33459

Merged
merged 1 commit into from
Jul 7, 2020
Merged

[8.x] Callable attributes are called with expanded definition #33459

merged 1 commit into from
Jul 7, 2020

Conversation

bastien-phi
Copy link
Contributor

On Laravel 7 and under, it is possible to define fatories like this :

$factory->define(MyModel::class, function(Faker $faker) {
    return [
        'total' => $faker->numberBetween(100, 1000),
        'part' => fn($attributes) => $faker->numberBetween(0, $attributes['total']),
        'rate' => fn($attributes) => $attributes['part'] / $attributes['total'],
    ];
});

Under Laravel 8, defining the factory with

public function definition() 
{
    return [
        'total' => $this->faker->numberBetween(100, 1000),
        'part' => fn($attributes) => $this->faker->numberBetween(0, $attributes['total']),
        'rate' => fn($attributes) => $attributes['part'] / $attributes['total'],
    ];
}

will fail because in rate callback, $attributes['part'] is the callback, not the actual value.

This example is maybe not very good but we have the same issue with

public function definition() 
{
    return [
        'user_id' => UserFactory::new(),
        'team_id' => fn($attributes) => User::find($attributes['user_id'])->team_id,
    ];
}

(here the $attributes['user_id'] is the factory)

@GrahamCampbell GrahamCampbell changed the title Callable attributes are called with expanded definition [8.x] Callable attributes are called with expanded definition Jul 7, 2020
@GrahamCampbell
Copy link
Member

Could you add a couple of tests, please?

@taylorotwell
Copy link
Member

I'm adding a few tests.

@taylorotwell taylorotwell merged commit c23da80 into laravel:master Jul 7, 2020
@bastien-phi bastien-phi deleted the update_expand_attributes branch July 8, 2020 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants