title: Using PHP Faker in Laravel to generate "unique with" entry when seeding a database using a factory
body: <p>So Similar to the unique with validation rule (See: <a href="https://github.com/felixkiss/uniquewith-validator" rel="noreferrer">https://github.com/felixkiss/uniquewith-validator</a>), I want to know how to generate a entry, where one column is unique with another one. I want to seed my database as follows.</p>
  
  <p>Example:</p>
  
  <p>There are 12 steps in the "steps" table. Each step should have 5 categories associated with each one that are stored in the "step_categories" table. Each of those categories are assigned a unique order number 1 through 5 that is unique with each "step_id".</p>
  
  <p>See this image here for an example of what the database should look like: <a href="https://imgur.com/a/XYA5yyn" rel="noreferrer">https://imgur.com/a/XYA5yyn</a></p>
  
  <p>I had to manually to make the entries in the database for the above image example. I don't want to have to generate this manually every time, say I make a mistake and have to rollback the migrations for example.</p>
  
  <p>I am using a factory to generate this data. So the factory name is <code>StepCategoriesFactory.php</code> and clearly I'm calling the factory with the <code>create()</code> method from the <code>DatabaseSeeder.php</code> file.</p>
  
  <p>I thought about doing this in a for loop, then i got as far as realizing when i called the <code>'step_id' =&gt; App\Model::all()-&gt;random()-&gt;id</code> to grab a new id, that I wouldn't be able to ensure I wasn't grabbing the id that i just generated 5 entries for. I'm really new with Laravel, and I'm not sure where to even start on this. There's no real information on SO where faker can use the unique with another column. How would I Go about this?</p>
  
  <p><strong>NOTE:</strong> The step id is not always going to be 1-12. The step ID might be different depending on whether a step gets deleted and remade. So just assigning the <code>step_id</code> to equal 1-12 wont work.</p>
  
  <p><strong>UPDATE:</strong> Here's some code I just wrote, and I think I'm on the right track. Maybe. I've grabbed the <code>step_id</code> by it's <code>number</code> field as that will always be 1-12, and I've grabbed the IID out of the entry. But now I'm stuck on how to generate the order 1-5 without repeating itself. I still haven't run this yet as its incomplete and I know it'll throw an error without the correct order number.</p>
  
  <p><strong>UPDATE 2:</strong> I think I'm on the right track here. However I'm getting an undefined variable error. When I put the first line from within the anonymous function, it's resetting the order to "1" for every entry. How do i make the $autoIncrement variable available to the anonymous function? The Seeder has stayed the same between updates.</p>
  
  <p>Image of the error: <a href="https://imgur.com/a/ywkd0Lb" rel="noreferrer">https://imgur.com/a/ywkd0Lb</a>
  Second image with the Die/Dump error in terminal: <a href="https://imgur.com/a/rOGRv32" rel="noreferrer">https://imgur.com/a/rOGRv32</a></p>
  
  <p>Reference this article here: <a href="https://laracasts.com/discuss/channels/laravel/model-factory-increment-value-faker?page=1" rel="noreferrer">https://laracasts.com/discuss/channels/laravel/model-factory-increment-value-faker?page=1</a></p>
  
  <p><strong>UPDATE 3:</strong> I forgot the <code>use ($autoIncrement)</code> line of code for the anonymous function. Code below has been updated, but now I'm getting a different error saying that the order column has a null value and can't be inserted. clearly it should be '1'. Even after I call my <code>$autoIncrement-&gt;next();</code> which should increment it to '1' it's still returning null according to the terminal. However, when I do a diedump on <code>$autoIncrement-&gt;current()</code> it's returning 1. Weird.</p>
  
  <p>Update 3 error: <a href="https://imgur.com/a/STOmIjF" rel="noreferrer">https://imgur.com/a/STOmIjF</a></p>
  
  <p><strong>StepCategoriesFactory.php</strong></p>
  
  <pre><code>use Faker\Generator as Faker;
  
  $autoIncrement = autoIncrement();
  
  $factory-&gt;define(App\StepCategory::class, function (Faker $faker) use ($autoIncrement) {
      // Generate Created At and Updated at DATETIME
      $DateTime = $faker-&gt;dateTime($max = 'now');
      $autoIncrement-&gt;next();
      $order = (int) $autoIncrement-&gt;current();
  
      return [
          // Generate Dummy Data
          'order' =&gt;  $order,
          'name' =&gt; $faker-&gt;words(4, true),
          'created_at' =&gt; $DateTime,
          'updated_at' =&gt; $DateTime,
      ];
  });
  
  function autoIncrement()
  {
      for ($i = 0; $i &lt; 5; $i++) {
          yield $i;
      }
  }
  </code></pre>
  
  <p>Edit: Put a bounty on this question, as I think it would be helpful for the community to get a detailed answer. I'm looking for help to explain how to go about making sure I'm grabbing the same entry through each loop.</p>