Nadeem Khan
Nadeem Khan

Reputation: 1

how to get data of other table in laravel 5.2

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.

In routes following is the code.

$addresses = App\Address::with('countries', 'states','cities')->get();
return $addresses;

when i am running the code i get an error

BadMethodCallException in Builder.php line 2161:
Call to undefined method Illuminate\Database\Query\Builder::countries()

Please help me.

cities table

Schema::create('cities', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('state_id')->unsigned();
        $table->foreign('state_id')->references('id')->on('states');
        $table->string('cityName', 50);
        $table->timestamps();
    });

states Table

 Schema::create('states', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('country_id')->unsigned();
            $table->foreign('country_id')->references('id')->on('countries');
            $table->string('stateName', 50);
            $table->timestamps();
        });

countries Table

Schema::create('countries', function (Blueprint $table) {
            $table->increments('id');
            $table->string('countryName', 50);
            $table->timestamps();
        });

addresses Table

Schema::create('addresses', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('country_id')->unsigned();
            $table->foreign('country_id')->references('id')->on('countries');
            $table->integer('state_id')->unsigned();
            $table->foreign('state_id')->references('id')->on('states');
            $table->integer('city_id')->unsigned();
            $table->foreign('city_id')->references('id')->on('cities');
            $table->string('firstLine', 50);
            $table->string('secondLine', 50);
            $table->string('thirdLine', 50);
            $table->string('zipCode', 50);
            $table->timestamps();
        });

and in models City

 class City extends Model
    {
        // City belongs to a State
        public function city(){
            return $this->hasOne('App\State');
        }

        public function address(){
            return $this->belongsTo('Address');
        }
    }

State

    class State extends Model
    {
        // State has many Cities
        public function cities(){
            return $this->hasMany('App\City');
        }

        // State belongs to a Country
        public function country(){
            return $this->hasOne('App\Country');
        }

    public function address(){
        return $this->belongsTo('Address');
    }
}

Country

    class Country extends Model
    {
        // Country has many States
        public function states(){
            return $this->hasMany('App\State');
        }

        public function address(){
            return $this->belongsTo('Address');
        }

}

Address

class Address extends Model
{
    // Address has one City
    public function cities(){
        return $this->hasOne('App\City','city_id');
    }

    // Address has one State
    public function states(){
        return $this->hasOne('App\State','state_id');
    }

    // Address has one Country
    public function countries(){
        return $this->hasOne('App\Country','country_id');
    }
}

Upvotes: 0

Views: 2628

Answers (1)

Yurich
Yurich

Reputation: 577

Now that we can access all of a post's comments, let's define a relationship to allow a comment to access its parent post. To define the inverse of a hasMany relationship, define a relationship function on the child model which calls the belongsTo method:

Look One To Many

You have one Country has many states and inverse relation is the state has one country. So you should in model State change method country() like this

public function country(){
    return $this->belongsTo('App\Country');
}

and use State::with('country')

P.S.

And check the other models.

Upvotes: 1

Related Questions