Вы сталкиваетесь с этой проблемой, когда имя вашей функции и одно из имен имен в файле одинаково. просто убедитесь, что все ваши имена имен в файле уникальны.
У @dparoli была хорошая идея.
Иногда для отношений «многие ко многим» используют наследуемый класс Pivot, но пока это не имеет значения.
Так что теперь мы можем использовать этот код ниже для списков.
$country = Country::findOrFail(1);
foreach ($country->countryStreet as $countryStreet){
echo $countryStreet->street->name
foreach($countryStreet->numbers as $number) {
$number;
}
}
class Countries
public function countryStreet()
{
return $this->hasMany(CountryStreet::class, 'country_id', 'id');
}
Class CountryStreet with method block() using relation hasOne
Еще одна реализация.
ИМХО с вашими таблицами я бы определил дополнительную модель: CountryStreet, что-то вроде этого:
// CountryStreet
class CountryStreet extends Model
{
public function city()
{
return $this->belongsTo('App\City');
}
public function street()
{
return $this->belongsTo('App\Street');
}
public function numbers()
{
return $this->belongsToMany('App\Number', 'number_country_street');
}
}
// Country
class Country extends Model
{
public function streets()
{
return $this->hasManyThrough('App\Street', 'App\CountryStreet');
}
}
// Streets
class Street extends Model
{
public function countries()
{
return $this->hasManyThrough('App\Country', 'App\CountryStreet');
}
}
// Number
class Number extends Model
{
public function country_streets()
{
return $this->belongsToMany('App\CountryStreet', 'number_country_street');
}
}