Framework: Laravel 8 factories missing afterCreatingState

Created on 10 Sep 2020  路  4Comments  路  Source: laravel/framework


  • Laravel Version: 8.0.0
  • PHP Version: 7.3.21
  • Database Driver & Version:

Description:

While converting my old tests to the new Factory class, I'm unable to find how to reproduce the afterCreatingState function.

Steps To Reproduce:

I tried the following:

public function admin() {
  $this->afterCreating(function (User $user) {
    $user->admin_user()->save(new AdminUser);
  }
  return $this->state([]);
}

but the afterCreating callback never fires. I don't know where else to put the block. Is this an oversight or am I missing something?

Most helpful comment

Read the docs a little more thoroughly, I missed that the callback must be returned:

public function admin() {
  return $this->afterCreating(function (User $user) {
    $user->admin_user()->save(new AdminUser);
  });
}

All 4 comments

Read the docs a little more thoroughly, I missed that the callback must be returned:

public function admin() {
  return $this->afterCreating(function (User $user) {
    $user->admin_user()->save(new AdminUser);
  });
}

Read the docs a little more thoroughly, I missed that the callback must be returned:

public function admin() {
  return $this->afterCreating(function (User $user) {
    $user->admin_user()->save(new AdminUser);
  }
}

You are missing ); at the fourth line but thanks anyway, it solved my problem :)

@jdavidbakr and if you want to compose multiple states on afterCreating ? More like "nested relationship multiple state".

@robsontenorio I'm not sure I understand what you are asking me?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhiloNL picture PhiloNL  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments