I am trying to Seed a database using a Model Factory in Laravel but am seeing this error on the terminal.
$ php artisan tinker Psy Shell v0.11.8 (PHP 8.1.0 — cli) by Justin Hileman >>> Parking::factory(1)->create(); [!] Aliasing 'Parking' to 'AppModelsParking' for this Tinker session. TypeError: IlluminateDatabaseGrammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in C:wamp64wwwmy-valetvendorlaravelframeworksrcIlluminateDatabaseQueryGrammarsGrammar.php on line 1010
From the multiple solutions I have tried, it seems the issue is at the faker-generated content level, but I need that faker content for the factory to work as intended.
Below are the Factory and Migration classes that will help you when trying to find the solution.
Here is my factory class
class ParkingFactory extends Factory { protected $model = Parking::class; /** * Define the model's default state. * * @return array */ public function definition(): array { return [ 'name' => $this->faker->words(2), 'description' => $this->faker->sentence, 'spaces' => rand(10, 100), 'physical_location' => $this->faker->streetAddress, 'latlng' => $this->faker->localCoordinates, 'base_pay' => $this->faker->numberBetween(100, 200), 'base_pay_breakdown' => $this->faker->sentence, 'rate_per_minute' => $this->faker->numberBetween(2, 10), 'overnight_base_pay' => $this->faker->numberBetween(100, 200), 'overnight_base_pay_breakdown' => $this->faker->sentence, 'overnight_rate_per_minute' => $this->faker->numberBetween(2, 10), 'other_charges_base_pay' => $this->faker->numberBetween(100, 200), 'other_charges_base_pay_breakdown' => $this->faker->sentence, 'other_charges_rate_per_minute' => $this->faker->numberBetween(2, 10), 'has_disability_accessibility' => $this->faker->boolean, 'has_other_charges' => $this->faker->boolean(1), 'has_overnight_parking' => $this->faker->boolean, 'overnight_charge_type' => $this->faker->randomElement(['none', 'flat', 'minute', 'both']), // 'owner_id' => User::factory()->create(), 'created_at' => now(), 'updated_at' => now() ]; } }
Here is the migration for the Parking table
Schema::create('parkings', function (Blueprint $table) { $table->id(); $table->string('name'); $table->mediumText('description'); $table->string('code') ->nullable(); $table->integer('spaces'); $table->string('physical_location') ->nullable(); $table->string('latlng')->nullable(); //charges info $table->decimal('base_pay') ->default(0.00); $table->string('base_pay_breakdown') ->nullable(); $table->decimal('rate_per_minute') ->default(0.00); $table->decimal('overnight_base_pay') ->default(0.00); $table->string('overnight_base_pay_breakdown') ->nullable(); $table->decimal('overnight_rate_per_minute') ->default(0.00); $table->decimal('other_charges_base_pay') ->default(0.00); $table->string('other_charges_base_pay_breakdown') ->nullable(); $table->decimal('other_charges_rate_per_minute') ->default(0.00); //charges info end $table->boolean('has_disability_accessibility') ->default(false); $table->boolean('has_other_charges') ->default(false); $table->boolean('has_overnight_parking') ->default(true); $table->string('overnight_charge_type') ->default('none'); //flat | minute | both $table->string('status') ->default('active'); $table->foreignId( 'owner_id') ->nullable() ->constrained('users') ->nullOnDelete(); $table->timestamps(); $table->softDeletes(); });
It is worth noting am using Laravel v9.24.0
, PHP v8.1.0
, and MySQL v8.0.27
.
What might be the cause of my error?
Advertisement
Answer
The name
field of your table is defined as a string
, however, the words
method of faker returns an array
.
Supplying the words
method with true
as a second argument will return two words as a string.
'name' => $this->faker->words(2, true);