【问题标题】:how to get data of other table in laravel 5.2如何在 laravel 5.2 中获取其他表的数据
【发布时间】:2016-05-29 04:54:23
【问题描述】:

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

【问题讨论】:

  • 看看你的 State 模型,它没有国家关系它有国家关系,所以 State::with('country')
  • 先生,但我的方法名称是地址模型中的“国家”。我仍然按照您所说的进行了更改,并且在 Connection.php 第 669 行中出现错误 QueryException:SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'countries.state_id'(SQL:select * from @ 987654332@ 其中countries.state_id 在 (1, 2, 3)))
  • 您的地址模型无关紧要,因为您使用的是App\State 类,因此在与该模型建立关系时使用该模型中的方法。您的state 模型也应该可能是belongTo('App\Country'),这就是您现在遇到另一个错误的原因。重新阅读文档laravel.com/docs/5.2/eloquent-relationships 可能会有所帮助
  • 我自己是 Laravel 的新手,实际上我正在开发一个具有相似关系结构的应用程序,就一系列 hasMany / belongsTo 模型而言。我发现使用 Eloquent 和 "::with()" 真的很笨拙。我最终使用正确的连接在原始 SQL 中编写了所有查询。
  • in model state change method country() 像这样 return $this->belongsTo('App\Country');

标签: laravel laravel-5.2


【解决方案1】:

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

Look One To Many

你有一个国家有很多州,相反的关系是一个国家有一个国家。 因此,您应该像这样在模型状态更改方法 country() 中

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

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

附言

并检查其他模型。

【讨论】:

  • 您好先生,感谢您的回答。但是我错误地写了错误的get query "$addresses = App\State::with('countries', 'states','cities')->get(); return $addresses;"我已更改为“$addresses = App\Address::with('countries', 'states','cities')->get(); return $addresses;”。我想获取与地址相关的所有城市、州、国家/地区详细信息
  • 我向你展示了规则。你只要跟着它,一切都会好起来的。
  • 我通过改变我所有模型中的关系得到了想要的结果。安装 $this->belongsTo('Address');在城市、州和国家模型中,我添加了 $this->hasOne('Address');在地址模型中,我放置了 $this->belongsTo('App\City');、$this->belongsTo('App\State');、$this->belongsTo('App\Country');。我的地址详细信息中包含国家、州、城市的所有相关详细信息
猜你喜欢
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2017-03-27
相关资源
最近更新 更多