title: How to seed multiple relationships in Laravel with Faker
body: <p>I have a database with two columns, brands and shops. Each brand can owe several shops, and I want to seed my database via Fakers using Laravel.</p>
  
  <p>So after setting up the migrations and the relationships in the models</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Brand extends Model
  {
  
      /**
       * Get the shops for the brand.
       */
      public function shops()
      {
          return $this-&gt;hasMany('App\Shop','sh_brand_id');
      }
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Shop extends Model
  {
      public function user() {
          return $this-&gt;belongsTo('App\Brand','sh_brand_id');
      }
  }
  </code></pre>
  
  <p>I want to use a Factory to seed the database. </p>
  
  <pre><code>&lt;?php
  
  use Faker\Generator as Faker;
  
  $factory-&gt;define(App\Shop::class, function (Faker $faker) {
  
      return [
          'name' =&gt; $faker-&gt;company,
          'address' =&gt; $faker-&gt;address,
      ];
  });
  </code></pre>
  
  <p>And</p>
  
  <p>
  
  <pre><code>use Faker\Generator as Faker;
  
      $factory-&gt;define(App\Brand::class, function (Faker $faker) {
  
          return [
              'name' =&gt; $faker-&gt;company,
              'logo_url' =&gt; $faker-&gt;imageUrl(640, 480),
              'website' =&gt; $faker-&gt;url,
              'description' =&gt; $faker-&gt;text(500),
              'telephone_number' =&gt;'31'. $faker-&gt;randomNumber(8),
              'principal_address' =&gt; $faker-&gt;address,
              'email' =&gt; $faker-&gt;unique()-&gt;safeEmail,
          ];
      });
  </code></pre>
  
  <p>And finally I need to seed the database using those Factories. There are documentation in the website and many examples for do it, but each solution I've found let me generate only one shop for each brand, and I want to generate many shops for each brands.</p>
  
  <p>What is the best way to do this?</p>