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