如何在laravel 5.2中获取其他表的数据

时间:2016-02-17 13:02:06

标签: laravel laravel-5.2

我是Laravel的新手,我正在开展项目,我想检索地址表的完整详细信息,包括城市名称,州名和国家名称。

以下路线是代码。

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

当我运行代码时出现错误

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

请帮帮我。

城市表

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();
    });

陈述表

 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();
        });

国家/地区表

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

地址表

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();
        });

和模特 的城市

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

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

国家

    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');
    }
}

国家

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

        public function address(){
            return $this->belongsTo('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');
    }
}

1 个答案:

答案 0 :(得分:1)

  

现在我们可以访问所有帖子的评论,让我们来定义一个   允许评论访问其父帖子的关系。界定   hasMany关系的逆,定义关系函数   在调用 belongsTo 方法的子模型上:

Look One To Many

你有一个国家有很多州和反对关系是国家有一个国家。 所以你应该在模型状态改变方法country()这样

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

并使用State::with('country')

P.S。

并检查其他型号。