I'm completely new to factories and seeds, and until now I've been manually adding data to each project in Sequel Pro or Workbench an.
I'm fairly new to Laravel in general, but I think I've got the models setup correctly.
I've got a recipe that can have multiple ingredients, and the ingredients can belong to multiple recipes.
The pivot between the two has a quantity and unit column (with the latter being nullable).
Recipe.php
class Recipe extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function ingredients(){
return $this->belongsToMany('App\Ingredient');
}
}
Ingredient.php
class Ingredient extends Model
{
public function recipes(){
return $this->belongsToMany('App\Recipe');
}
}
IngredientFactory.php
$factory->define(App\Ingredient::class, function (Faker $faker) {
return [
'name' => $faker->word,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
];
});
RecipeFactory.php
$factory->define(App\Recipe::class, function (Faker $faker) {
$userIds = User::all()->pluck('id')->toArray();
return [
'title' => $faker->text(30),
'description' => $faker->text(200),
'user_id' => $faker->randomElement($userIds),
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
];
});
IngredientTableSeeder
public function run()
{
factory(App\Ingredient::class, 100)->create();
}
RecipeTableSeeder
public function run()
{
factory(App\Recipe::class, 30)->create();
}
That all works fine, but I'm trying to work out how I can generate data for the pivot table.
I feel like it should be something like this:
RecipeIngredientsFactory.php
$factory->define(?::class, function (Faker $faker) {
$recipeIds = Recipe::all()->pluck('id')->toArray();
$ingredientIds = Ingredient::all()->pluck('id')->toArray();
return [
'recipe_id' => $faker->randomElement($recipeIds),
'ingredient_id' => $faker->randomElement($ingredientIds),
];
});
But I'd be unsure what to put as the ?
for the model::class
?
I could be completely off base, but the logic seems right.
Please let me know in the comments if any more info is required.