Я новичок в Laravel, и я работаю над проектом, где я хочу получить полную информацию о адресной таблице, включая название города, название штата и название страны.как получить данные из другой таблицы в laravel 5.2
В следующих маршрутах приведен код.
$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();
});
и в моделях 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');
}
}
Государственные
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');
}
}
Посмотрите на свою модель государства, у нее есть отношения между странами, у нее есть отношение страны, так что государство :: с ('страна') –
сэр, но мое имя метода - это «страны» в адресной модели. все еще я изменил его, как вы сказали, и получив ошибку QueryException в строке Connection.php 669: SQLSTATE [42S22]: Столбец не найден: 1054 Неизвестный столбец 'countries.state_id' в 'where clause' (SQL: select * из 'стран', где' countries'.state_id' в (1, 2, 3)) –
Ваша модель адреса не имеет значения, потому что вы используете класс 'App \ State', поэтому вы используете методы в этой модели, когда строить отношения с этой моделью. Ваша модель 'state' также должна, вероятно,« принадлежать »(« App \ Country »), поэтому вы получаете еще одну ошибку. Это может помочь перечитать документы https://laravel.com/docs/5.2/eloquent-relationships – user3158900