Given that I have the following tables:
- users
- questions
- tags
- question_tag
my pivot table with two fields: question_id & tag_id
and my App\Question model has the following relationships:
class Question extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function tags()
{
return $this->hasMany(Tag::class);
}
}
I've created the following factories:
database/factories/UserFactory.php
$factory->define(App\User::class, function (Faker $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('123456')
];
});
database/factories/QuestionFactory.php
$factory->define(App\Question::class, function (Faker $faker) {
static $user_id;
return [
'user_id' => $user_id,
'subject' => $faker->sentence(15),
'body' => $faker->paragraph(10)
];
});
database/factories/TagFactory.php
$factory->define(App\Tag::class, function (Faker $faker) {
return [
'name' => str_slug($faker->words(mt_rand(1, 2))),
'description' => $faker->sentence()
];
});
I tried to use it all together in my dummy data seeder like this:
class DummyDataSeeder extends Seeder
{
public function run()
{
// Seed dummy users
factory(App\User::class, 10)->create()->each(function($user)
{
// With dummy questions
$user->questions()->saveMany(factory(App\Question::class, 10)->make()->each(function($question)
{
// With dummy tags
$question->tags()->sync(factory(App\Tag::class, 3)->make());
}));
});
}
}
When I run the seeder, I get the following error:
[ErrorException] mb_strtolower() expects parameter 1 to be string, array given
Is this not possible to do in model factory? Do I need to use a different approach?