title: how to get data of other table in laravel 5.2
body: <p>I am new to Laravel and i am working on the project where i want to retrieve the full details of address table including city name, state name and country name.</p>
  
  <p>In routes following is the code.</p>
  
  <pre><code>$addresses = App\Address::with('countries', 'states','cities')-&gt;get();
  return $addresses;
  </code></pre>
  
  <p>when i am running the code i get an error</p>
  
  <pre><code>BadMethodCallException in Builder.php line 2161:
  Call to undefined method Illuminate\Database\Query\Builder::countries()
  </code></pre>
  
  <p>Please help me.</p>
  
  <p><strong>cities table</strong></p>
  
  <pre><code>Schema::create('cities', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('state_id')-&gt;unsigned();
          $table-&gt;foreign('state_id')-&gt;references('id')-&gt;on('states');
          $table-&gt;string('cityName', 50);
          $table-&gt;timestamps();
      });
  </code></pre>
  
  <p><strong>states Table</strong></p>
  
  <pre><code> Schema::create('states', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('country_id')-&gt;unsigned();
              $table-&gt;foreign('country_id')-&gt;references('id')-&gt;on('countries');
              $table-&gt;string('stateName', 50);
              $table-&gt;timestamps();
          });
  </code></pre>
  
  <p><strong>countries Table</strong></p>
  
  <pre><code>Schema::create('countries', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('countryName', 50);
              $table-&gt;timestamps();
          });
  </code></pre>
  
  <p><strong>addresses Table</strong></p>
  
  <pre><code>Schema::create('addresses', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('country_id')-&gt;unsigned();
              $table-&gt;foreign('country_id')-&gt;references('id')-&gt;on('countries');
              $table-&gt;integer('state_id')-&gt;unsigned();
              $table-&gt;foreign('state_id')-&gt;references('id')-&gt;on('states');
              $table-&gt;integer('city_id')-&gt;unsigned();
              $table-&gt;foreign('city_id')-&gt;references('id')-&gt;on('cities');
              $table-&gt;string('firstLine', 50);
              $table-&gt;string('secondLine', 50);
              $table-&gt;string('thirdLine', 50);
              $table-&gt;string('zipCode', 50);
              $table-&gt;timestamps();
          });
  </code></pre>
  
  <p>and in models
  <strong>City</strong></p>
  
  <pre><code> class City extends Model
      {
          // City belongs to a State
          public function city(){
              return $this-&gt;hasOne('App\State');
          }
  
          public function address(){
              return $this-&gt;belongsTo('Address');
          }
      }
  </code></pre>
  
  <p><strong>State</strong></p>
  
  <pre><code>    class State extends Model
      {
          // State has many Cities
          public function cities(){
              return $this-&gt;hasMany('App\City');
          }
  
          // State belongs to a Country
          public function country(){
              return $this-&gt;hasOne('App\Country');
          }
  
      public function address(){
          return $this-&gt;belongsTo('Address');
      }
  }
  </code></pre>
  
  <p><strong>Country</strong></p>
  
  <pre><code>    class Country extends Model
      {
          // Country has many States
          public function states(){
              return $this-&gt;hasMany('App\State');
          }
  
          public function address(){
              return $this-&gt;belongsTo('Address');
          }
  
  }
  </code></pre>
  
  <p><strong>Address</strong></p>
  
  <pre><code>class Address extends Model
  {
      // Address has one City
      public function cities(){
          return $this-&gt;hasOne('App\City','city_id');
      }
  
      // Address has one State
      public function states(){
          return $this-&gt;hasOne('App\State','state_id');
      }
  
      // Address has one Country
      public function countries(){
          return $this-&gt;hasOne('App\Country','country_id');
      }
  }
  </code></pre>