title: How to add Relationship of Factories to faker in Laravel
body: <p>I have models like this:</p>
  
  <pre><code>Entity - entities table
  Address - address table
  User - users table
  Geoloc - geoloc table
  UserEntity - entity_user table
  </code></pre>
  
  <p>Now Entity can have one address and one geoloc, but it can have multiple users this is what UserEntity is for.</p>
  
  <p>So I need to do a pattern where factory will create it like this:</p>
  
  <p>User>Entity>Address>Geoloc>UserEntity</p>
  
  <p>so when user is created it will also create entity, address and geoloc, associate entity id to address, associate geoloc id to entity and insert user_id and entity_id to user entity.</p>
  
  <p>Here's my current setup:</p>
  
  <p>AddressFactory:</p>
  
  <pre><code>$factory-&gt;define(App\Address::class, function (Faker $faker) {
      return [
          'building_name' =&gt; $faker-&gt;company,
          'address' =&gt; $faker-&gt;streetAddress,
          'town' =&gt; $faker-&gt;city,
          'postcode' =&gt; $faker-&gt;postcode,
          'telephone' =&gt; $faker-&gt;phoneNumber,
          'private' =&gt; $faker-&gt;boolean($chanceOfGettingTrue = 50),
          'entity_id' =&gt; function () {
              return factory(App\Entity::class)-&gt;create()-&gt;id;
          }
      ];
  });
  </code></pre>
  
  <p>EntityFactory:</p>
  
  <pre><code>factory-&gt;define(App\Entity::class, function (Faker $faker) {
      return [
          'name' =&gt; $faker-&gt;unique()-&gt;company,
          'type' =&gt; 'Service',
          'tags' =&gt; 'mens clothes, online shopping, ladies clothes',
          'email' =&gt; $faker-&gt;safeEmail,
          'logo' =&gt; $faker-&gt;imageUrl($width = 640, $height = 480),
          'cover' =&gt; $faker-&gt;imageUrl($width = 1000, $height = 600),
          'bio' =&gt; $faker-&gt;catchPhrase,
          'main' =&gt; $faker-&gt;realText($maxNbChars = 200, $indexSize = 2),
          'twitter_business' =&gt; 'OfficialOAFC',
          'facebook_business' =&gt; 'MolinoLounge',
          'instagram_business' =&gt; 'OfficialOAFC',
          'google_places' =&gt; 'ChIJvaCBHYG3e0gRfhs0xZ1TBB8',
          'eventbrite' =&gt;'8252340832',
          'featured' =&gt; $faker-&gt;boolean($chanceOfGettingTrue = 50),
          'url' =&gt; $faker-&gt;url,
          'video' =&gt; '6R2DIFySho4',
          'wikipedia' =&gt; 'Hackerspace',
          'contact_name' =&gt; $faker-&gt;name,
          'contact_number' =&gt; $faker-&gt;phoneNumber,
          'geoloc_id' =&gt; function () {
              return factory(App\Geoloc::class)-&gt;create()-&gt;id;
          }
      ];
  });
  </code></pre>
  
  <p>UserFactory:</p>
  
  <pre><code>$factory-&gt;define(App\User::class, function (Faker $faker) {
      static $password;
      return [
          'name' =&gt; $faker-&gt;name,
          'email' =&gt; $faker-&gt;unique()-&gt;safeEmail,
          'password' =&gt; $password ?: $password = bcrypt('secret'),
          'avatar' =&gt; $faker-&gt;imageUrl($width = 640, $height = 480),
          //'telephone' =&gt; $faker-&gt;phoneNumber,
          'role' =&gt; rand(1, 2),
          'remember_token' =&gt; str_random(10),
      ];
  });
  </code></pre>
  
  <p>DatabaseSeeder:</p>
  
  <pre><code>    factory(App\User::class, 30)-&gt;create();
      factory(App\Address::class, 30)-&gt;create();
  </code></pre>
  
  <p>User relations:</p>
  
  <pre><code>public function entities()
  {
      return $this-&gt;belongsToMany('App\Entity', 'entity_user', 'user_id', 'entity_id');
  }
  </code></pre>
  
  <p>Entity relations:</p>
  
  <pre><code>public function address()
  {
      return $this-&gt;hasOne('App\Address', 'entity_id');
  }
  public function _geoloc()
  {
      return $this-&gt;belongsTo('App\Geoloc', 'geoloc_id', 'id');
  }
  </code></pre>