hjortur17's avatar

Factory failing when connecting hasMany relationship

I'm trying to make my first factory for my model Car. Each time I try to run it I get Argument #1 ($values) must be of type array, string given. CarFactory.php:71.

I do have hasMany relationship. Category and Cars are connected like this:

public function cars()
{
     return $this->hasMany(Car::class);
}

I think my Factory is failing when I try to connect a car to a category. This is my factory:

return [
            'slug' => Str::of($carSlug)->slug('-'),
            'manufacturer' => $carMani,
            'model' => $carModel,
            'range' => $this->faker->numberBetween(120, 650),
            'drive' => $this->faker->shuffleArray(['AWD', 'RWD', 'FWD']),
            'seating' => $this->faker->numberBetween(3, 5),
            'luggage' => $this->faker->numberBetween(1, 6),
            'description' => $this->faker->text,
            'category_id' => Category::factory()->count(1)->create()->id,
            'image_folder_url' => $this->faker->url,
            'price_per_day' => $this->faker->randomNumber(5),
];

And this is the seeder:

Car::factory()->has(Category::factory()->count(1))->count(50)->create();
0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Never give a count() if you want 1 instance. You will get a collection

'category_id' => Category::factory()->create()->id,

//or
'category_id' => Category::factory(),

And the seeder if you want to overwrite it as you are doing

Car::factory()->for(Category::factory())->count(50)->create();
1 like
hjortur17's avatar

@Sinnbeck - When I try to do 'category_id' => Category::factory() I get this error: Call to undefined method App\Models\Car::category().

And if I try this Category::factory()->create()->id, I get this error: Argument #1 ($values) must be of type array, string given

Should I somehow do my relationship in any other way?

Sinnbeck's avatar

@hjortur17 It sounds like you dont have a category() method? Also did you fix the seeder code like I showed?

hjortur17's avatar

@Sinnbeck - Yes, I get this error with the seeder code: Argument #1 ($values) must be of type array, string given

I do not have a category method on the Car model. I do have cars method on Category model

Sinnbeck's avatar

@hjortur17 I suggest you add it to the model. I don't think you can start from Car unless it's there. You need to start from the category then

hjortur17's avatar

@Sinnbeck - So now I'm running into different issue. Now I get this error Array to string conversion when running this seeder code:

Car::factory()->has(Category::factory())->count(50)->create();

I have added belongsToMany relationship to Car model (with category_id column in the cars table). Still getting this issue. Any idea what could be wrong now?

public function categories()
{
    return $this->belongsToMany(Category::class);
}
Sinnbeck's avatar

@hjortur17 if cars has a category_id, it means it's belongsTo.

public function category()
{
    return $this->belongsTo(Category::class);
}

And it's like this in the seeder

Car::factory()->for(Category::factory())->count(50)->create();
1 like
zubair12341's avatar

There you need to change the query

From Car::factory()->has(Category::factory()->count(1))->count(50)->create();

To

Car::factory()->for(Category::factory())->count(50)->create();

Hope this will be better

Please or to participate in this conversation.