【发布时间】: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