I need to run the factory 50
times, so within DatabseSeeder
:
public function run() { for($i=1;$i<=50;$i++){ (new CategoryQuestionFactory($i))->create(); } }
As you can see, I tried passing a variable named $i
as a parameter to the CategoryQuestionFactory
class.
Then in this factory I tried this:
class CategoryQuestionFactory extends Factory { protected $counter; public function __construct($c) { $this->counter = $c; } /** * Define the model's default state. * * @return array<string, mixed> */ public function definition() { $question = Question::find($this->counter); return [ 'category_id' => $this->faker->numberBetween(1,22), 'question_id' => $question->id ]; } }
But when I run php artisan db:seed
in the terminal, I get this error:
Call member function pipeline() on null
exist C:xampphtdocsforumrootvendorlaravelframeworksrcIlluminateDatabaseEloquentFactoriesFactory.php:429
So what’s the problem here? How to correctly send values as parameters to factory classes?
Additionally, in the IDE for the factory's __construct
method, I get the following message:
The following is the error capture in the IDE:
It seems to me that you want to seed the intermediate table. There are a few methods you can use when seeding, one of them is
has()
, which is the method I often use.Suppose you want to create 100 questions and 5 categories